Fetching remote pages

The two main reasons why I need to fetch a remote page are either to further transform the data there or for some sort of API call.

Derived resources from your HTML

Web pages these days are rather dynamic. Shopping carts, purchase orders, timetables — the list goes on. These are all pages presented to the person visiting your site, their content transient. It can be useful to capture that page and derive something more permanent from it. Setting a print css can be useful for your visitor, but if you want to have a trail you could create a PDF of the page and email it. This is useful for receipts and invoices.

API calls

Many sites that act like a resource (especially resource management) often have an API to allow you to poke and peek at your data in there.

All this means that you may want to fetch a remote page (even if it is your own). Here is a simple example.

$tcbfeed = wp_remote_get( 'http://www.tcbarrett.com/feed/' );
if ( is_wp_error( $tcbfeed  ) ) {
   echo 'Something went wrong!';
} else {
echo 'TCB Feed:
' . print_r( $tcbfeed, 1 ) . '
';
}

If you put this in a page template, you should see how it works.