Reputation: 11
I'm learning PHP and I wanted to know how to download a specific DIV every one or two hour automatically via cURL from a website.
Upvotes: 0
Views: 2031
Reputation: 2210
UNTESTED (may be errors):
set_time_limit(3600*24); //24 hours
$numDownloads = 12;
for ($i = 0; $i < $numDownloads; $i++)
{
$ch = curl_init('http://www.example.com/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$content = curl_exec($ch);
curl_close($ch);
if (preg_match('/<div>(.*?)<\/div>/i', $content, $matches))
{
$divContents = $matches[1];
$myFile = 'div'.$i.'.txt';
if ($fh = fopen($myFile, 'w'))
{
fwrite($fh, $divContents)
}
fclose($fh);
}
sleep(3600*2);
}
I suggest you drop the loop and run that on a cron job though....
Upvotes: 1
Reputation: 25572
You can download the page with cURL, and parse it with SimpleXML to find what you need.
SimpleXMLElement::xpath
might be the fastest way to find what you are looking for.
Upvotes: 0
Reputation: 798516
You don't. You download the whole page, then parse it for the specific DIV you're interested in.
Upvotes: 2