Fetching remote web pages with curl and PHP

A drawing of a cartoon man pointing upwards

Heads up! This post was written in 2008, so it may contain information that is no longer accurate. I keep posts like this around for historical purposes and to prevent link rot, so please keep this in mind as you're reading.

— Cory

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

$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.