开发者

PHP cURL Warning during curl_multi_add_handle

开发者 https://www.devze.com 2022-12-17 03:01 出处:网络
My PHP code $urlArray = array(\'http://firsturl.com\', \'http://secondurl.com\'); $nodeCount = count($urlArray);

My PHP code

    $urlArray = array('http://firsturl.com', 'http://secondurl.com');
    $nodeCount = count($urlArray);
    $chContainter = array();
    $mh = curl_multi_init();
    for($i = 0; $i < $nodeCount; $i++) {
        $chContainter[$i] = curl开发者_开发知识库_init();
        curl_setopt($chContainter[$i], CURLOPT_URL, $urlArray[$i]);
        curl_setopt($chContainter[$i], CURLOPT_HEADER, 0);
        curl_setopt($chContainter[$i], CURLOPT_RETURNTRANSFER,1);
        curl_multi_add_handle($mh,$chContainter[$i]);
    } 

is generating the following warning

Warning: (null)(): 4 is not a valid cURL handle resource in Unknown on line 0

Warning: (null)(): 5 is not a valid cURL handle resource in Unknown on line 0

I did some debugging and found out the warning was generated when I try to add curl handle to the $mh.

Please help. Thanks.


Make sure to initialize curl_multi_init() only after you initialized your curl_init() sessions, otherwise you'll get that error.


I just resolved this problem on one of my own scripts. The issue was caused by URLs with unencoded spaces, for example: http://example.com/space here/. I resolved the issue by replacing any plain space with %20, like so: http://example.com/space%20here/

From other reading online, it seems like this error can occur for any reason that makes the URL inaccessible or otherwise malformed.


In manual (see example #1) shows that curl_multi_init() calls after curl_init(). So this should work:

$urlArray = array('http://firsturl.com', 'http://secondurl.com');
$nodeCount = count($urlArray);
$chContainter = array();
for ($i = 0; $i < $nodeCount; $i++) {
    $chContainter[$i] = curl_init();
    curl_setopt($chContainter[$i], CURLOPT_URL, $urlArray[$i]);
    curl_setopt($chContainter[$i], CURLOPT_HEADER, 0);
    curl_setopt($chContainter[$i], CURLOPT_RETURNTRANSFER,1);
}

$mh = curl_multi_init();
for ($i = 0; $i < $nodeCount; $i++) {
    curl_multi_add_handle($mh,$chContainter[$i]);
}

Not very elegant, but it solved my similar problem.


I just encounter this problem

my code:

$url = 'www.meilishuo.com';
$queue = curl_multi_init();
$ci = createCh($url);
$ciSec = createCh($url);
curl_multi_add_handle($queue, $ci);
curl_multi_add_handle($queue, $ciSec);

function createCh($url) {
    $ci = curl_init();
    /* Curl settings */
    curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 5);
    curl_setopt($ci, CURLOPT_NOSIGNAL,1);
    curl_setopt($ci, CURLOPT_TIMEOUT, 1);
    curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ci, CURLOPT_HEADER, 0);
    curl_setopt($ci, CURLOPT_URL, $url);
    curl_setopt($ci, CURLOPT_FOLLOWLOCATION,1);
    return $ci;
}

the warning:

PHP Warning: (null)(): 34 is not a valid cURL handle resource in Unknown on line 0

PHP Warning: (null)(): 35 is not a valid cURL handle resource in Unknown on line 0

PHP Warning: (null)(): 34 is not a valid cURL handle resource in Unknown on line 0

PHP Warning: (null)(): 35 is not a valid cURL handle resource in Unknown on line 0

fix:

I add it at finally:

    curl_multi_close($queue);

problem resolved, but I don't understand, why it resolved after I add it.

what meaning is it?

"Unknown on line 0"(the part of warning)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号