Running script in background using php curl

2019-08-16 04:04发布

问题:

I want to run a function as background process using curl. Below is my code.

  foreach ($iles $file=> $size) {

                $params ="file=$file&fullpath=$fullpath&minWidth=$minWidth";
                $url = 'http://test.rul.com/file/listFiles?'.$params;
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
                $curled=curl_exec($ch);
                curl_close($ch);
            }
        }


     public function getlistFiles() {
 $fullpath = $_REQUEST['fullpath'];
 }

but this curl is not running on background. how can I execute this as background ?

回答1:

Here is an example for the calling script with curl:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 400); 
curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
$response = curl_exec($ch);
curl_close($ch);

2nd script

ignore_user_abort(true);
usleep(500000);    // wait 500ms
// do stuff

Note that you will always get a curl error CURLE_OPERATION_TIMEDOUT, which can be ignored.