Friday 2 December 2011

How to retrieve remote page data in wordpress?

Wordpress will have an inbuilt function to get data from a remote page which is located in wp-includes/http.php.
Here is the syntax of the function:



$url is the url of the page from which we want to get data.

It returns the array of information which includes headers and content.
Use the following code where you want to get the page data.

$response = wp_remote_get( 'http://foo.com/file.txt' );
if( is_wp_error( $response ) ) {
echo 'Something went wrong!';
} else {
echo 'Response:';
print_r( $response );
echo '';
}


The above code will print the array like the below format

Array
(
[headers] => Array
(
[date] => Thu, 30 Sep 2010 15:16:36 GMT
[server] => Apache
[x-powered-by] => PHP/5.3.3
[x-server] => 10.90.6.243
[expires] => Thu, 30 Sep 2010 03:16:36 GMT
[cache-control] => Array
(
[0] => no-store, no-cache, must-revalidate
[1] => post-check=0, pre-check=0
)

[vary] => Accept-Encoding
[content-length] => 1641
[connection] => close
[content-type] => application/php
)
[body] => This is a website!
[response] => Array
(
[code] => 200
[message] => OK
)

[cookies] => Array
(
)

)