0

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 ?

7
  • this works. but you need to force a curl timeout. and make sure the called script continues running. Commented Feb 4, 2018 at 18:59
  • Possible duplicate of Continue PHP execution after sending HTTP response Commented Feb 4, 2018 at 19:00
  • check out this answer. that works great: stackoverflow.com/a/41263257/4379151 Commented Feb 4, 2018 at 19:00
  • 2nd script must use ignore_user_abort(true); Commented Feb 4, 2018 at 19:04
  • @ErikKalkoken curl didnt work after adding timeout , any reasons ? Commented Feb 4, 2018 at 19:12

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.