有一个解析robots.txt的任务。我正在使用 php curl。在某些网站上,我收到类似的回复
cURL Error (28): Operation timed out after 30001 milliseconds with 0 bytes received
增加超时并不能解决问题。解析代码本身:
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
$robots = curl_exec($ch);
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
curl_close($ch);
将您的行添加到您的代码中:
curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
https://www.adidas.ru/robots.txt - 默认使用 gzip 提供,因为您可以看到其他一些您遇到问题的 robots.txt。
UPD
第二种选择是通过传递标头来要求不要使用 gzip 向服务器提供内容
curl_setopt($ch, CURLOPT_HTTPHEADER, ['accept-encoding: deflate, br']);
这是如果突然有人在程序集/版本中遇到 gzip 问题
可能的原因之一。
CURL - FAQ - 4.1 连接到 SSL 服务器时的问题。
有时我们在使用或
curl连接到SSL服务器时遇到问题。SSLeayOpenSSL v0.9+许多较旧的 SSL 服务器无法使用
SSLv3. 要解决此问题,请在命令行中添加curl选项--sslv2。有时远程服务器不喜欢该请求
SSLv2并且应该使用SSLv3. 命令行选项--sslv3。在 ubuntu 16.04 对 sslv3 连接有限制,显然服务器禁止另一个版本 https://ubuntugeeks.com/questions/33156/simple-way-of-enabling-sslv2-and-sslv3-in-openssl