How to Retrieve the Content of a Remote Webpage from a WordPress Site

Posted by admin 20 September 2011

WordPress has a function that allows you to retrieve the content of a remote webpage so you can do something with it. There are two functions that you can use:

  • wp_remote_get (Retrieve the webpage content using HTTP GET)
  • wp_remote_post (Retrieve the webpage content using HTTP POST)

Example Code to Retrieve Remote Webpage Content using HTTP GET Method


$response = wp_remote_get( 'http://www.example.com/some-text-file.txt' );
if(is_wp_error($response))//check to see if any error occured
{
echo 'An error orccured when trying to retrieve the remote webpage content!';
print_r($response); //This will dump more data
}
else //webpage content was retrieved successfully
{
echo 'Here is the retrieved content:';
echo '<pre>';
print_r($response);
echo '</pre>';
}

Example Code to Retrieve Remote Webpage Content using HTTP POST Method


$response = wp_remote_post( 'http://www.example.com/some-text-file.txt' );
if(is_wp_error($response))//check to see if any error occured
{
echo 'An error orccured when trying to retrieve the remote webpage content!';
print_r($response); //This will dump more data
}
else //webpage content was retrieved successfully
{
echo 'Here is the retrieved content:';
echo '<pre>';
print_r($response);
echo '</pre>';
}

I hope this neat WordPress trick to retrieve remote webpage content helps.

Related posts:

  1. How To Add Custom Content After Each Post In WordPress
Tags: , , ,

Leave your response!

You can subscribe to these comments via RSS.