Fetching Remote Web Pages With CURL and PHP

A note from A Beautiful Site’s founder: We developed Surreal to be easy for you and your clients. If you’re a web designer, you should take a look at the simple, hosted CMS that’s changing the way content is managed. Surreal integrates in moments and is trusted by over 18,000 websites. Try it out for free and let us know what you think! Visit Website »

This is a very brief example of how to use PHP’s cURL Library to retrieve the source of a remote webpage.

<?php
$c = curl_init();
curl_setopt($c, CURLOPT_URL, "http://example.com/");
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($c);
curl_close($c);
?>

CURLOPT_RETURNTRANSFER is a predefined constant that tells cURL to return the output to a variable instead of displaying it in the browser. Visit the PHP Manual for a list of all CURL predefined constants and their uses.

The source of the remote file will be stored as a string in $data.

To use the cURL library, you must have the cURL PHP extension installed. See the PHP Manual for information about Installing CURL.

If you enjoyed this article, please share it with a friend!

One Response to Fetching Remote Web Pages With CURL and PHP

  1. Personally I really enjoy using Snoopy, this way you don’t need to have CURL installed on the server. Alot of service providers infact don’t.

    http://snoopy.sourceforge.net/

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>