php - 我应该关闭 cURL 吗?

标签 php performance curl

我有一个函数使用 cURL 多次调用 3 个不同的 API。每个 API 的结果都会传递给嵌套循环中调用的下一个 API,因此 cURL 当前打开和关闭超过 500 次。

我应该让 cURL 对整个函数保持打开状态,还是可以在一个函数中多次打开和关闭它?

最佳答案

重复使用同一个句柄可以提高性能。见:Reusing the same curl handle. Big performance increase?

如果您不需要同步请求,请考虑使用 curl_multi_* 函数(例如 curl_multi_initcurl_multi_exec 等),它们也可以大幅提升性能。

更新:

我尝试为每个请求使用新句柄并使用以下代码使用相同的句柄来代替 curl:

ob_start(); //Trying to avoid setting as many curl options as possible
$start_time = microtime(true);
for ($i = 0; $i < 100; ++$i) {
    $rand = rand();
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://www.google.com/?rand=" . $rand);
    curl_exec($ch);
    curl_close($ch);
}
$end_time = microtime(true);
ob_end_clean();
echo 'Curl without handle reuse: ' . ($end_time - $start_time) . '<br>';

ob_start(); //Trying to avoid setting as many curl options as possible
$start_time = microtime(true);
$ch = curl_init();
for ($i = 0; $i < 100; ++$i) {
    $rand = rand();
    curl_setopt($ch, CURLOPT_URL, "http://www.google.com/?rand=" . $rand);
    curl_exec($ch);
}
curl_close($ch);
$end_time = microtime(true);
ob_end_clean();
echo 'Curl with handle reuse: ' . ($end_time - $start_time) . '<br>';

得到以下结果:

Curl without handle reuse: 8.5690529346466
Curl with handle reuse: 5.3703031539917

因此,当多次连接到同一个服务器时,重复使用同一个句柄实际上可以显着提高性能。我尝试连接到不同的服务器:

$url_arr = array(
    'http://www.google.com/',
    'http://www.bing.com/',
    'http://www.yahoo.com/',
    'http://www.slashdot.org/',
    'http://www.stackoverflow.com/',
    'http://github.com/',
    'http://www.harvard.edu/',
    'http://www.gamefaqs.com/',
    'http://www.mangaupdates.com/',
    'http://www.cnn.com/'
);
ob_start(); //Trying to avoid setting as many curl options as possible
$start_time = microtime(true);
foreach ($url_arr as $url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_exec($ch);
    curl_close($ch);
}
$end_time = microtime(true);
ob_end_clean();
echo 'Curl without handle reuse: ' . ($end_time - $start_time) . '<br>';

ob_start(); //Trying to avoid setting as many curl options as possible
$start_time = microtime(true);
$ch = curl_init();
foreach ($url_arr as $url) {
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_exec($ch);
}
curl_close($ch);
$end_time = microtime(true);
ob_end_clean();
echo 'Curl with handle reuse: ' . ($end_time - $start_time) . '<br>';

得到以下结果:

Curl without handle reuse: 3.7672290802002
Curl with handle reuse: 3.0146431922913

仍然有相当大的性能提升。

关于php - 我应该关闭 cURL 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18046637/

相关文章:

PHP SQL UPDATE 数千行中每行一个字段,每个字段都在 php 中构建

javascript - 使用 AJAX 执行通过 PHP 交付的 javascript 代码

javascript - 为什么 IE8 用 Ja​​vaScript 渲染页面无法使用,而 FF 却表现良好?

c++ - 对结构进行操作并将结果分配给同一结构是不好的做法吗?为什么?

curl - 我如何看到带有 curl 的预告片标题

php - 根据表单响应发送到特定 URL

php - 我什么时候/应该在 PHP 中使用 __construct()、__get()、__set() 和 __call()?

c# - 什么对解析 : regular expressions or linq? 更好

authentication - Curl 无法通过 sftp 密码身份验证

json - Linux grep命令从json中查找key的值