dongzhoutuo0883 2018-12-04 20:34
浏览 71
已采纳

php丢失了curl句柄参考

At the bottom of the code snippet, I am attempting to remove the curl handle from the multi handle. However PHP reports that it is an invalid curl handle. the curl_close call reports the same thing. I am confused since I have not closed it above that point.

Am i losing it anywhere. I don't see where...

    foreach ($urls as $url) {

        $request = [];
        $request['url'] = $url;
        $request['body'] = '';
        $request['response_headers'] = [];
        $request['curl_handle'] = curl_init();

        $url['config'] = json_decode($url['config'], true);

        if($url['config']['method'] == 'GET') {
            curl_setopt($request['curl_handle'], CURLOPT_HTTPGET, true);
        }

        curl_setopt($request['curl_handle'], CURLOPT_URL, $url['source_url']);

        curl_setopt($request['curl_handle'], CURLOPT_WRITEFUNCTION, function($curl, $body) use (&$request) {
            $request['body'] .= $body;
            return strlen($body);
        });
        curl_setopt($request['curl_handle'], CURLOPT_HEADERFUNCTION, function($curl, $header) use (&$request) {
            $request['response_headers'][] = $header;
            return strlen($header);
        });

        $followRedirects = boolval($url['config']['follow_redirects']);
        curl_setopt($request['curl_handle'], CURLOPT_FOLLOWLOCATION, $followRedirects);  

        curl_setopt($request['curl_handle'], CURLOPT_CONNECTTIMEOUT, 10);  
        curl_setopt($request['curl_handle'], CURLOPT_TIMEOUT, 120);

        curl_setopt($request['curl_handle'], CURLOPT_MAXREDIRS, intval($url['config']['total_redirects']));

        curl_setopt($request['curl_handle'], CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);

        curl_setopt($request['curl_handle'], CURLOPT_MAXFILESIZE, intval($url['config']['max_download']));

        curl_setopt($request['curl_handle'], CURLOPT_SSL_VERIFYHOST, false);  
        curl_setopt($request['curl_handle'], CURLOPT_SSL_VERIFYPEER, false);  

        $requests[] = &$request;
    }

    $mh = curl_multi_init();

    //add the handles
    foreach ($requests as &$request) {
        curl_multi_add_handle($mh, $request['curl_handle']);
    }

    $active = null;
    //execute the handles
    do {
        $mrc = curl_multi_exec($mh, $active);
        print('after exec');
    } while ($mrc == CURLM_CALL_MULTI_PERFORM);

    while ($active && $mrc == CURLM_OK) {            
        $mrc = curl_multi_exec($mh, $active);

        if (curl_multi_select($mh) != -1) {
            do {
                $mrc = curl_multi_exec($mh, $active);
                print('performing again');
            } while ($mrc == CURLM_CALL_MULTI_PERFORM);
        }
    }

    if ($mrc != CURLM_OK) {
        print("Curl multi read error $mrc
");
    }

    foreach ($requests as &$request) {
        processResponse($request);

        curl_multi_remove_handle($mh, $request['curl_handle']);
        curl_close($request['curl_handle']);
    }

    curl_multi_close($mh);
  • 写回答

2条回答 默认 最新

  • dongtao4890 2018-12-05 12:33
    关注

    The issue was that $request was being copied by reference into $requests, so the same curl_handle was being passed through multi curl and then to close_handle multiple times. The fix was not setting $request by reference into $requests.

    I changed :

    $requests[] = &$request;
    

    to

    $requests[] = $request;
    

    That solved my issue.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 MATLAB APP 制作出现问题
  • ¥15 wannier复现图像时berry曲率极值点与高对称点严重偏移
  • ¥15 利用决策森林为什么会出现这样·的问题(关键词-情感分析)
  • ¥15 DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI[/untitled30_war_e
  • ¥15 使用deepspeed训练,发现想要训练的参数没有梯度
  • ¥15 寻找一块做为智能割草机的驱动板(标签-stm32|关键词-m3)
  • ¥15 信息管理系统的查找和排序
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),怎么用读取的电磁传感器信号表示小车所在的位置
  • ¥15 如何解决y_true和y_predict数据类型不匹配的问题(相关搜索:机器学习)
  • ¥15 PB中矩阵文本型数据的总计问题。