dongsu2807 2019-03-22 22:49
浏览 176

PHP Curl请求运行脚本直到结束,但是获取超时错误

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
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 c程序不知道为什么得不到结果
    • ¥40 复杂的限制性的商函数处理
    • ¥15 程序不包含适用于入口点的静态Main方法
    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置