I have a PHP script (script 1) that is called from a website using AJAX. The script will use Curl to call another PHP script (script 2) on the server. THAT script will then use Curl as well to run another PHP script (script 3).
The first curl call will be executed (execute script 2). However, script 3 always gives back time out error (but script 3 actually executes and does what it is supposed to do). Its just that no response is given back to script 2 if script 2 was executed using curl from script 1.
If I start script 2 in the web browser then script 3 will execute and response will be given back to script 2 and no timeout will occur.
All scripts are on the same server, and I get no error message in the log other than error related to the timeout.
Code below show how script 1 starts multiple instances of script 2. This part works fine, I have checked by printing out info to a text file from script 2 and I can verify that all instances of script 2 runs/finish and also that they all try to execute script 3 and that they all receive the same timeout error.
foreach ($urls as $url) {
$requests[] = curl_init($url);
curl_setopt($requests[count($requests) - 1], CURLOPT_RETURNTRANSFER, true);
}
$mh = curl_multi_init();
foreach ($requests as $request) {
curl_multi_add_handle($mh, $request);
}
$running = null;
do {
curl_multi_exec($mh, $running);
} while ($running);
$responses = array();
foreach ($requests as $request) {
$responses[] = curl_multi_getcontent($request);
}
foreach ($requests as $request) {
curl_multi_remove_handle($mh, $request);
}
curl_multi_close($mh);
Code chunk below is how script 2 executes script 3, this will work fine if I run script 2 in browser, will take about 5 seconds and output will be printed in browser window with no errors whatsoever.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_PORT, 80);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
echo $output;
curl_close($ch);
If script 2 is started from script 1 I will have this error when script 2 tries to run script 3 (number of milliseconds will always match my value for CURLOPT_TIMEOUT)
Operation timed out after 60000 milliseconds with 0 bytes received 03/22/2019 06:13:04 pm