Jump to content

Asynchronous script


Aegidius

Recommended Posts

I have an heavy script to be executed.

 

If I put the code as main script the user will see a loading blank page for a while, until he can keep to navigate the site.

 

I'd like that the main script throw a background script that execute the heave code, while the user can keep to navigate the site.

Link to comment
https://forums.phpfreaks.com/topic/235090-asynchronous-script/#findComment-1208298
Share on other sites

you could launch a background process

function launchBackgroundProcess($call) {
     // Windows
    if(is_windows()){
        pclose(popen(’start /b ‘.$call.”, ‘r’));
    }
     // Some sort of UNIX
    else {
        pclose(popen($call.‘ /dev/null &’, ‘r’));
    }
    return true;
}

function is_windows(){
    if(PHP_OS == ‘WINNT’ || PHP_OS == ‘WIN32′){
        return true;
    }
    return false;
}

 

Link to comment
https://forums.phpfreaks.com/topic/235090-asynchronous-script/#findComment-1208311
Share on other sites

this function will submit an asynchronous POST query to the specified url

function backgroundPost($url){
  $parts=parse_url($url);

  $fp = fsockopen($parts['host'], 
          isset($parts['port'])?$parts['port']:80, 
          $errno, $errstr, 30);

  if (!$fp) {
      return false;
  } else {
      $out = "POST ".$parts['path']." HTTP/1.1\r\n";
      $out.= "Host: ".$parts['host']."\r\n";
      $out.= "Content-Type: application/x-www-form-urlencoded\r\n";
      $out.= "Content-Length: ".strlen($parts['query'])."\r\n";
      $out.= "Connection: Close\r\n\r\n";
      if (isset($parts['query'])) $out.= $parts['query'];

      fwrite($fp, $out);
      fclose($fp);
      return true;
  }
}

//Example of use
backgroundPost('http://example.com/slow.php?file='.
                       urlencode('some file.dat'));

 

should work for ya

 

Link to comment
https://forums.phpfreaks.com/topic/235090-asynchronous-script/#findComment-1208320
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.