Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute Curl Multi Exec without waiting for a response

Tags:

php

curl

I'm trying to send off a request to resize an image to an API I made. After I send the request, I serve up an un-resized image while I wait for the resized image to be made. Is there a way make a curl request but then not wait for the response so I can quickly serve up an image?

This is my current (blocking) solution:

$mh_curl = curl_multi_init();
$ch = curl_init();
//POST request
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_multi_add_handle($mh_curl, $ch);
$running = NULL;
//execute curl handles
do {
curl_multi_exec($mh_curl, $running);
curl_multi_select($mh_curl);      
} while ($running > 0);
//close the handles
curl_multi_remove_handle($mh_curl, $ch);
curl_multi_close($mh_curl);
like image 824
Caleb Avatar asked Oct 18 '25 17:10

Caleb


1 Answers

Try:

$cmd = 'curl -s "http://my.domain/script.php"';
exec($cmd . " > /dev/null &");

This will lead the curl result to nirvana

like image 129
jb7AN Avatar answered Oct 20 '25 09:10

jb7AN