Lets say we want to call a webpage from a site and store it in our server for further use. Why should we want to do something like that? Because for example this webpage has varius infos, or many, that we want to pull out and store them in our database for further use. A good case is to recycle the infos and with a link (backlink) or something like that post them in our blog, add them as further resources in a article that we work etc etc the possibilities are endless! I used to collect videos, flash games, photos, images and descriptions for a funny site that I run, then with a cron job I had this website updated asap with a cron job and for a long time without doing anything else. Nice huh?

In this post Ill saw you how you can do that. First of all the majority of hosting companies let you use Curl for this call as its secure for their server. So if Curl doesnt work its very possible that you cant complete the above case. Dreamhost where I host my sites has a unique tutorial for this, many infos and of course it let you use it. So lets start:

//—First of all we give the timeout of the script running (10secs are ok) and the webpage that we want to pull, set to zero for no timeout.
$timeout = 10;
$webpage = “http://www.domain.com/webpage.php”;

//—After that we open a curl command.
$ch = curl_init();

//—We call the page
curl_setopt ($ch, CURLOPT_URL, $webpage);

//—We tell the script not to send headers to the domain.com server.
curl_setopt($ch, CURLOPT_HEADER, false);

//—We setting the returntransefer so the script it will return the result on success, FALSE on failure. Just to know what we are doing.
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

//—We are setting the timeout.
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

//—We are telling below to the script to store the page in the $content variable.
$content = curl_exec($ch);

//—We are closing the Curl command
curl_close($ch);

//—Now we are choosing were to store the content that we pulled
$filename = ‘text.txt’;

//—Optional we are chmoding the page
chmod($filename, 0777);

//—We write the page to the text file.
if (!$handle = fopen($filename, ‘a’)) {
echo “Cannot open file ($filename)”;
exit;
}
if (fwrite($handle, $content) === FALSE) {
echo “Cannot write to file ($filename)”;
exit;
}

//—We are closing the handle.
fclose($handle);
?>

Thats it now we have the webpage http://www.domain.com/webpage.php stored in text.txt in our server and we can manipulate it!

More infos you can find in php.net strongly recommend it.


Share This