必须在句柄关闭之前调用curl_close()两次并且可以读取cookie jar.这是一个错误吗?

Nat*_*ate 11 php curl curl-multi

我试图理解为什么cURL的cookie jar文件在我尝试阅读时是空的时候,我一直在靠墙撞击墙.我刚刚发现,如果我调用curl_close()两次而不是一次,我的代码可以工作,但是,我想知道这是否是cURL的错误.

这是一个例子:

curl_close($chInfo['handle']);
var_dump(is_resource($chInfo['handle']));
Run Code Online (Sandbox Code Playgroud)

那输出boolean true.因此,换句话说,尽管我打电话,手柄仍未关闭curl_close().

我的下一个想法是,可能需要一些时间来关闭句柄,所以我sleep()curl_close()通话后尝试使用了几秒钟,但没有任何区别.

出于绝望,我试着复制这curl_close()条线,像这样:

curl_close($chInfo['handle']);
curl_close($chInfo['handle']);
var_dump(is_resource($chInfo['handle']));
Run Code Online (Sandbox Code Playgroud)

那个输出boolean false,意味着句柄被关闭,我能够从cookie jar文件中读取(当句柄关闭时,cURL将cookie写入文件).

那么这里发生了什么?这似乎非常像一个错误!

编辑:我无法发布我的完整代码(你不会想要阅读它!),但这是一个简化的例子(请注意,在这个例子中只提取了一个网址,而在我的实际代码curl_multi中用于获取同时有很多网址):

$curlOptions = array(
    CURLOPT_USERAGENT      => 'Mozilla/5.001 (windows; U; NT4.0; en-US; rv:1.0) Gecko/25250101',
    CURLOPT_CONNECTTIMEOUT => 5, // the number of seconds to wait while trying to connect.
    CURLOPT_TIMEOUT        => 5, // the maximum number of seconds to allow cURL functions to execute.
    CURLOPT_RETURNTRANSFER => 1, // TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
    CURLOPT_FOLLOWLOCATION => 1,
    CURLOPT_MAXREDIRS      => 10,
    CURLOPT_AUTOREFERER    => 1,
    CURLOPT_REFERER        => null,
    CURLOPT_POST           => 0,  // GET request by default
    CURLOPT_POSTFIELDS     => '', // no POST data by default
    CURLINFO_HEADER_OUT    => 1, // allows the request header to be retrieved
    CURLOPT_HEADER         => 1, // returns the response header along with the page body
    CURLOPT_URL            => 'http://www.example.com/',
    CURLOPT_COOKIEJAR      => __DIR__ . '/cookie.txt',
    CURLOPT_COOKIEFILE     => __DIR__ . '/cookie.txt'
);
Run Code Online (Sandbox Code Playgroud)


$ch = curl_init();
curl_setopt_array($ch, $curlOptions); // set the options for this handle

$mh = curl_multi_init();
$responses = array();
curl_multi_add_handle($mh, $ch); // add the handle to the curl_multi object

do
{
    $result   = curl_multi_exec($mh, $running);
    $activity = curl_multi_select($mh);    // blocks until there's activity on the curl_multi connection (in which case it returns a number > 0), or until 1 sec has passed

    while($chInfo = curl_multi_info_read($mh))
    {
        $chStatus = curl_getinfo($chInfo['handle']);

        if($chStatus['http_code'] == 200) // if the page was retrieved successfully
        {
            $response = curl_multi_getcontent($chInfo['handle']); // get the response

            curl_multi_remove_handle($mh, $chInfo['handle']); // remove the curl handle that was just completed
            curl_close($chInfo['handle']);                    // close the curl handle that was just completed (cookies are saved when the handle is closed?)
            curl_close($chInfo['handle']);

            var_dump(is_resource($chInfo['handle']));
        }
        else // request failed
        {
            echo 'Error: Request failed with http_code: ' . $chStatus['http_code'] . ', curl error: ' . curl_error($chInfo['handle']). PHP_EOL;
        }
    }
} while ($running > 0);

curl_multi_close($mh);
Run Code Online (Sandbox Code Playgroud)

如果运行上面的代码,输出将是

boolean false
Run Code Online (Sandbox Code Playgroud)

表示手柄已关闭.但是,如果删除第二次调用curl_close(),则输出将更改为

boolean true
Run Code Online (Sandbox Code Playgroud)

指示手柄关闭.

Hug*_*ing 5

这不是一个错误,而是它的工作方式.如果查看源代码,您可以看到发生了什么.

首先打开手柄$ch = curl_init();并查看源,ext\curl\interface.c你可以看到内部设置ch->uses = 0;

然后你打电话curl_multi_add_handle($mh, $ch);,看看ext\curl\multi.c这个方法呢ch->uses++;.在此刻ch->uses==1

现在,最后一部分,curl_close($chInfo['handle']);再看一遍,ext\curl\interface.c它有以下代码:

if (ch->uses) {
    ch->uses--;
} else {
    zend_list_delete(Z_LVAL_P(zid));
}
Run Code Online (Sandbox Code Playgroud)

因此,关闭它的第一次尝试将减少ch->uses,第二次尝试将关闭它.

此内部指针仅在使用curl_multi_add_handle或使用时增加curl_copy_handle.所以我想这个想法是curl_multi_add_handle使用句柄的副本而不是实际的句柄.