0

I would like to know if, and how, I can call or execute what otherwise would be known as a cron job

If I were to enter this crontab through my host cron manager interface it would look like:

wget "http://somedomain.com/index.php?option=csome_option&view=some_view&key=some_key&format=some_format"

Is it possible to run this http call everytime a page is loaded (using PHP)?

I do not want to display the outcome of this call, but rather simply execute what this call would get going if I were to call directly through a browser, but (inevitably sounding redundant) without displaying simply executing behind the curtains on cue from page load.

Thank you,

Book Of Zeus
  • 49,509
  • 18
  • 174
  • 171
IberoMedia
  • 2,226
  • 7
  • 36
  • 61

2 Answers2

2

If you are on a shared server that doesn't allow fopen, another trick is to add something like this to the page

<div style="display:hidden">
    <img src="/myphpscript.php" />
</div>

This will cause the php script to be run. You may also do well to have myphpscript.php output image headers so that the browser doesn't complain.

Michael Fenwick
  • 2,374
  • 2
  • 19
  • 28
  • I like this approach even better though I was under the impression the OP wasn't in control of the remote script. To match mime types, an `` tag could be more appropriate – Phil Oct 12 '11 at 03:23
  • As I think more about it, there are a few more things that you should do to improve the solution above. Either you could add some AJAX which inserts the above in the page after the document ready event, or have the php script send back its headers early. Otherwise, you risk the browser holding up the loading of the page while it waits for the php to finish running. – Michael Fenwick Oct 12 '11 at 03:28
  • AJAX won't work if the domain differs. In any case, it's going to be a blocking operation though images and other HTML assets are loaded in parallel by browsers. – Phil Oct 12 '11 at 03:33
  • Sticking something like `ob_start(); $size = ob_get_length(); header("Content-Length: $size"); header('Connection: close'); ob_end_flush(); ob_flush(); flush();` at the top of the myphpscript.php file should get it to return right away I believe, rather than wait for the entire php script to complete, so it shouldn't block the page noticeably. – Michael Fenwick Oct 12 '11 at 15:20
1

Assuming you have the fopen HTTP wrapper enabled...

$fp = fopen(
    'http://somedomain.com/index.php?option=csome_option&view=some_view&key=some_key&format=some_format',
    'rb');
if ($fp !== false) {
    fclose($fp);
}
Phil
  • 157,677
  • 23
  • 242
  • 245
  • yes this worked. The script is executed, but then the primary page is not loading fully. and I imagine that this calls for a new question... je je je – IberoMedia Oct 12 '11 at 02:20