【问题标题】:PHP cURL: Reset Callback (CURLOPT_HEADERFUNCTION)PHP cURL:重置回调(CURLOPT_HEADERFUNCTION)
【发布时间】:2022-01-07 00:19:01
【问题描述】:

我有一个类似下面的代码:

$ch = curl_init();
// ...
curl_setopt($ch, CURLOPT_HEADERFUNCTION, function ($ch, $header_line) {
    // process $header_line...
    return strlen($header_line); // needed by curl
});
// ...
curl_exec($ch);

在该代码之后,我想继续使用$ch 来重新使用连接,但我不再需要自定义的CURLOPT_HEADERFUNCTION 回调。我怎样才能重置它?我尝试将其设置为null,但没有成功。

【问题讨论】:

标签: php curl php-curl


【解决方案1】:

在数组中设置常用选项并在执行之间重置选项:

$common_options = [
    CURLOPT_XXX => 'something'
    // ...
];
$ch = curl_init();

// First call: common + specific options
curl_setopt_array($ch, $common_options);
curl_setopt($ch, CURLOPT_FOO, 'something else');

curl_exec($ch);

// Second call: common options only
curl_reset($ch);
curl_setopt_array($ch, $common_options);

curl_exec($ch);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-15
    • 1970-01-01
    • 2018-10-05
    • 1970-01-01
    • 2014-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多