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条回答 默认 最新

相关推荐 更多相似问题