ZetCode

PHP curl_reset 函数

最后修改日期:2025 年 4 月 11 日

PHP 的 curl_reset 函数会重置 cURL 会话的所有选项。它允许在清除先前设置的同时,为多个请求重复使用同一个 cURL 句柄。这对于性能优化非常有用。

基本定义

curl_reset 函数会将给定 cURL 句柄上设置的所有选项重置为其默认值。它不会关闭会话或更改 URL。

语法:curl_reset(CurlHandle $handle): void。该函数只接受一个 cURL 句柄作为参数。它不返回任何值,也不抛出任何错误。

基本的 curl_reset 用法

此示例展示了如何在请求之间重置 cURL 句柄。

basic_reset.php
<?php

declare(strict_types=1);

$ch = curl_init();

// First request
curl_setopt($ch, CURLOPT_URL, "https://api.example.com/data");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response1 = curl_exec($ch);

// Reset for second request
curl_reset($ch);

// Second request with different options
curl_setopt($ch, CURLOPT_URL, "https://api.example.com/users");
curl_setopt($ch, CURLOPT_POST, true);
$response2 = curl_exec($ch);

curl_close($ch);

我们使用同一个 cURL 句柄发出两个不同的请求。第一个请求后,我们重置所有选项。这确保没有设置会传递到第二个请求。句柄在请求之间保持打开状态。

错误后重置

此示例演示了在请求失败后重置 cURL 句柄。

error_reset.php
<?php

declare(strict_types=1);

$ch = curl_init();

// First request with invalid URL
curl_setopt($ch, CURLOPT_URL, "https://invalid.example");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);

if ($response === false) {
    echo "First request failed: " . curl_error($ch);
    curl_reset($ch); // Reset after failure
    
    // Retry with valid URL
    curl_setopt($ch, CURLOPT_URL, "https://valid.example.com");
    $response = curl_exec($ch);
    
    if ($response !== false) {
        echo "Retry succeeded";
    }
}

curl_close($ch);

在请求失败后,我们在重试前重置句柄。这会清除任何错误状态和之前的选项。重试将使用新的设置进行干净的尝试。重置后,错误处理仍然有效。

使用不同方法重用句柄

此示例展示了如何为 GET 和 POST 请求重用 cURL 句柄。

method_reset.php
<?php

declare(strict_types=1);

$ch = curl_init();

// GET request
curl_setopt($ch, CURLOPT_URL, "https://api.example.com/data");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$getResponse = curl_exec($ch);

// Reset for POST request
curl_reset($ch);

// POST request
curl_setopt($ch, CURLOPT_URL, "https://api.example.com/data");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, ['key' =>  'value']);
$postResponse = curl_exec($ch);

curl_close($ch);

我们使用同一个句柄执行 GET 请求,然后是 POST 请求。重置确保 GET 选项不会影响 POST 请求。两个请求都有干净的配置。这种模式对于多个连续请求来说效率很高。

使用自定义标头重置

此示例演示了使用自定义标头重置句柄。

header_reset.php
<?php

declare(strict_types=1);

$ch = curl_init();

// Request with JSON headers
curl_setopt($ch, CURLOPT_URL, "https://api.example.com/json");
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/json',
    'Accept: application/json'
]);
$jsonResponse = curl_exec($ch);

// Reset for XML request
curl_reset($ch);

// Request with XML headers
curl_setopt($ch, CURLOPT_URL, "https://api.example.com/xml");
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    'Content-Type: application/xml',
    'Accept: application/xml'
]);
$xmlResponse = curl_exec($ch);

curl_close($ch);

我们使用不同的 Content-Type 标头发出请求。重置会清除所有之前的标头。每个请求都有其自身的特定标头,而不会受到干扰。这对于需要不同 Content-Type 的 API 非常有用。

性能比较

此示例比较了带重置和不带重置的句柄重用。

performance.php
<?php

declare(strict_types=1);

// Without reset (recreating handle)
$start = microtime(true);
for ($i = 0; $i < 100; $i++) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://api.example.com/test");
    curl_exec($ch);
    curl_close($ch);
}
$time1 = microtime(true) - $start;

// With reset (reusing handle)
$start = microtime(true);
$ch = curl_init();
for ($i = 0; $i < 100; $i++) {
    curl_reset($ch);
    curl_setopt($ch, CURLOPT_URL, "https://api.example.com/test");
    curl_exec($ch);
}
curl_close($ch);
$time2 = microtime(true) - $start;

echo "Without reset: $time1 seconds\n";
echo "With reset: $time2 seconds\n";

此基准测试显示了重用句柄的性能优势。重置版本避免了重复初始化。对于多个请求,带重置的句柄重用通常更快。随着请求数量的增加,差异也会增大。

最佳实践

来源

PHP curl_reset 文档

本教程通过实际示例介绍了 PHP curl_reset 函数,展示了其在高效 cURL 句柄管理方面的用法。

作者

我叫 Jan Bodnar,是一名充满热情的程序员,拥有丰富的编程经验。我自 2007 年以来一直撰写编程文章。迄今为止,我已撰写了 1400 多篇文章和 8 本电子书。我在编程教学方面拥有十多年的经验。

所有 PHP cURL 教程列表。