1

I'm trying to have my page refresh ever 5 seconds to update data. Though Cron only runs every 1 minute. So I tried to incorporate into the page I'm loading a self refresh every 5 seconds.

My Cron job is: /usr/bin/php /home/cplpc/public_html/commons/btc-fetch.php

This seems to update my code, though only once every minute.

The btc-fetch.php page I've created is:

<?php
    $page = $_SERVER['PHP_SELF'];
    $sec = "5";
 ?>
<html>
<head>
<meta http-equiv="refresh" content="<?php echo $sec?>;URL='<?php echo $page?>'">
<meta name="robots" content="noindex">
<meta name="googlebot" content="noindex">
</head>
<body>
<?php
    $url = "https://www.bitstamp.net/api/ticker";
    $fgc = file_get_contents($url);
    file_put_contents('/home/cplpc/public_html/data/btc-the-price', $fgc);
    echo $fgc;
?>
</body>

Any guidance or direction would be greatly appreciated. This script works if I have btc-fetch.php open in my browser, though I assume this is because 'it's in a browser'? Is there anyway to achieve the same desired task?

* Thanks for the replies

I'm using cPanel for the Cron Job. Perhaps I need to look at another way then PHP_SELF refresh. Thanks for the tips.

aussiedan
  • 329
  • 1
  • 9

1 Answers1

1

You can do it with cronjobs, but it's probbaly not a very good approach.

* * * * * /usr/bin/php /home/cplpc/public_html/commons/btc-fetch.php
* * * * * sleep 5 && /usr/bin/php /home/cplpc/public_html/commons/btc-fetch.php
* * * * * sleep 10 && /usr/bin/php /home/cplpc/public_html/commons/btc-fetch.php
* * * * * sleep 15 && /usr/bin/php /home/cplpc/public_html/commons/btc-fetch.php
* * * * * sleep 20 && /usr/bin/php /home/cplpc/public_html/commons/btc-fetch.php
* * * * * sleep 30 && /usr/bin/php /home/cplpc/public_html/commons/btc-fetch.php
* * * * * sleep 35 && /usr/bin/php /home/cplpc/public_html/commons/btc-fetch.php
* * * * * sleep 40 && /usr/bin/php /home/cplpc/public_html/commons/btc-fetch.php
* * * * * sleep 45 && /usr/bin/php /home/cplpc/public_html/commons/btc-fetch.php
* * * * * sleep 50 && /usr/bin/php /home/cplpc/public_html/commons/btc-fetch.php
* * * * * sleep 55 && /usr/bin/php /home/cplpc/public_html/commons/btc-fetch.php

The catch: if your PHP script runs for longer then 5 seconds, you will end up with a lot of used (blocked) resources.

Idea taken from https://stackoverflow.com/a/9619449/9618184

Binar Web
  • 672
  • 1
  • 7
  • 20