-2

How to automatically and periodically save from this page to my web server with PHP?

http://example.com/page.html

You can make a web page scraper script in PHP, then schedule it to run using cron jobs.

<?php
// Call this file 'scraper.php'
$local_file = "local.html"; // local destination
$remote_file = "http://example.com/page.html"; // source
$ch = curl_init();
$fp = fopen ($local_file, 'w+');
$ch = curl_init($remote_file);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_ENCODING, "");
curl_exec($ch);
curl_close($ch);
fclose($fp);?>  

You can make a web page scraper script in PHP, then schedule it to run using cron jobs.

To automatically run this script every day, make a cron job something like this

* 0 * * * /usr/bin/php /root/document/path/scraper.php

*where scraper.php is the name of the code block above, /usr/bin/php is the server path to the PHP interpreter, and * 0 * * * are timing parameters that make the script run once per day. *

If you want to save the webpage to your server as a local file you could do it like this:

$html = file_get_contents("http://example.com/page.html");file_put_contents("/path/to/local.html", $html);

below code not working pleas fix it

if (!file_exists('/local_mis.php')) {
mkdir('/causelist/madras/causelist/misc/', 0777, true);}

2 Answers2

1

You can make a web page scraper script in PHP, then schedule it to run using cron jobs.

<?php
    // Call this file 'scraper.php'
    $local_file = "local.html"; // local destination
    $remote_file = "http://example.com/page.html"; // source

    $ch = curl_init();
    $fp = fopen ($local_file, 'w+');
    $ch = curl_init($remote_file);
    curl_setopt($ch, CURLOPT_TIMEOUT, 50);
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_ENCODING, "");
    curl_exec($ch);
    curl_close($ch);
    fclose($fp);
?>

To automatically run this script every day, make a cron job something like this

* 0 * * * /usr/bin/php /root/document/path/scraper.php

where scraper.php is the name of the code block above, /usr/bin/php is the server path to the PHP interpreter, and * 0 * * * are timing parameters that make the script run once per day.

All servers are different, and I don't know what server you are running, but here are instructions for setting cron jobs in Ubunutu: https://askubuntu.com/questions/2368/how-do-i-set-up-a-cron-job

Community
  • 1
  • 1
Drakes
  • 23,254
  • 3
  • 51
  • 94
  • $local_file = "local.html"; how to made own location folder – Gnana lingam Apr 16 '15 at 20:25
  • 1
    I need you to give a clear example of what "automatic" means in this case. Please give an example of where you want the data from that web page to be saved on your local server – Drakes Apr 17 '15 at 04:49
  • above script automatic Running on date and time schedule – Gnana lingam Apr 17 '15 at 05:31
  • 1
    Do you know about cron jobs? That's what you are into now. Completely outside of PHP. If you don't know, then I can provide some links. Cron jobs are the best way for automatic script execution on a schedule. – Drakes Apr 17 '15 at 05:32
  • I've added information on how to schedule this script – Drakes Apr 17 '15 at 05:42
  • i am not know cron jobs pleas provide script – Gnana lingam Apr 17 '15 at 08:57
  • 1
    That is the 'script'. That's it. That's all :) I don't know your server, but all of them let you schedule cron jobs. For example, on ubuntu, here are instructions: http://askubuntu.com/questions/2368/how-do-i-set-up-a-cron-job – Drakes Apr 17 '15 at 09:08
0

If you want to save the webpage to your server as a local file you could do it like this:

$html = file_get_contents("http://example.com/page.html");
file_put_contents("/path/to/local.html", $html);
PKeidel
  • 2,559
  • 1
  • 21
  • 29