How to retrieve a remote page using WordPress

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.

Looking for WordPress hosting? Try WP Web Host. Prices starts at $5/month and you can try it for free!

How to retrieve a remote page using WordPress

Leave a Reply

Your email address will not be published. Required fields are marked *