有什么办法可以加速 cURL

Is there any way to speed up cURL

我想知道是否有任何方法可以通过 PHP 加速这样的 cURL 请求。

<?php
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://www.randomsite.com/path');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.79 Safari/537.36');
$html = trim(curl_exec($ch));

echo $html;

curl_close($ch);
?>

我循环了几百次,花了很长时间,所以我想知道是否有任何方法可以加快这个过程。

是的,您可以做的第一件事就是启用压缩,如果您要获取的内容从压缩中受益(这不包括 jpg/png/gif/anything 预压缩,但像 html/css/javascript/xml 这样的内容受益匪浅来自传输压缩)- 将 CURLOPT_ENCODING 设置为空字符串,让 curl 自动处理传输压缩。

I'm looping this a few hundred times and it's taking pretty long - 除非由于某种原因不能同时获取它们,只需使用 curl_multi api,那么你可以同时获取数百个,应该是 显着 比使用 curl_exec 方法(只能连续获取它们)