Tuesday, September 9, 2008

Write Site content into a file using CURL

/*
This CURL program is for opening a given URL page
and write the content into a file

Example :
URL ==> http://google.com
file ==> testing.txt
*/

/* Site URL */
$url = "http://google.com";

/* Write the Content into a file */
$filename = 'testing.txt';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$somecontent = curl_exec ($ch);
$xml = curl_exec ($ch);
curl_close ($ch);

// In our example we're opening $filename in write mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($filename, 'w')) {
echo "Cannot open file ($filename)";
exit;
}

// Write $somecontent to our opened file.
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}

echo "Success, wrote ($somecontent) to file ($filename)";

fclose($handle);
?>

No comments:

Popular Posts