fsockopen question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tomfra
Forum Contributor
Posts: 126
Joined: Wed Jun 23, 2004 12:56 pm
Location: Prague, Czech Republic

fsockopen question

Post by tomfra »

I am using this code to execute one command:

Code: Select all

$fp = fsockopen($domain, 80, $errno, $errstr);
    
    if (!$fp) { echo "$errstr ($errno)<br>\n"; } else {
			
    $out = "GET ".$_SERVER['PHP_SELF']."?com=cache&pass=$password HTTP/1.1\r\n";
    $out .= "Host: $domain\r\n";
    $out .= "Connection: Close\r\n\r\n";

    fwrite($fp, $out);

    while (!feof($fp)){
      echo fgets($fp,32000); 
    }
This code works, it does indeed sends the GET request properly. But when I execute this code directly from the web browser, the "com=cache" command is beign processed in the real time just like if I executed it directly and not through fsockopen.

What I'd like to do is to 'force' the server to send the GET request, finish the job on the server and then notify me via email (for example) when the job is done. The reason for this is that the "cache" command can take several hours to finish.

I hope I explained it good enough... The point is, the work should be done "silently" on the server just like if the code was executed through Cron job.

Thanks!

Tomas
hedge
Forum Contributor
Posts: 234
Joined: Fri Aug 30, 2002 10:19 am
Location: Calgary, AB, Canada

Post by hedge »

It sounds like you want it to run Asynchronously. You'd have to have your script submit a background job like a cron job or something of that nature.
tomfra
Forum Contributor
Posts: 126
Joined: Wed Jun 23, 2004 12:56 pm
Location: Prague, Czech Republic

Post by tomfra »

But is there a way to create a new cron job or its alternative via PHP? I've heard something about the Pseudo Cron job class but that one is licenced under GPL which I can't combine with the script I am working on and I may not need all of its power either.

Tomas
tomfra
Forum Contributor
Posts: 126
Joined: Wed Jun 23, 2004 12:56 pm
Location: Prague, Czech Republic

Post by tomfra »

On a similar note, is it possible to allow a given PHP script to be executed through Cron job only and not through web browser?

Tomas
Post Reply