Post Pic

How to retrieve a remote page using WordPress

Do you want to be able to get the content, as well as file info, of a remote file in order to use it on your WordPress install? Today’s recipe will let you know how you can do it easily.

Just use the wp_remote_get() function (Or wp_remote_post() is you prefer using the POST method) to retrieve the desired url. The example below shows how to retrieve an url and display its content as well as the file info.

This code can be used anywhere on your template files.

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

Credits: WordPress Codex.

2 Responses

Sep 16 2011 02:41

Can wp_remote_get also retrieve and display web page/s? Thanks.

Jan 23 2012 09:39

I want to display .php page (output of it, not code) from remote site

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required

WP Theme of the week

Sponsored Likebox