我想获得使用curl的网站内容,这是Cloudflare的服务背后。以下代码适用于大多数网站,但不是Cloudflare背后的代码,甚至在cloudflare.com上都失败了。你能帮助我解决可能导致这种行为的原因吗?
我有以下配置:
代码:
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,5);
curl_setopt($ch, CURLOPT_TIMEOUT, 5); //timeout in seconds
curl_setopt($ch, CURLOPT_URL, "https://cloudflare.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
//curl_setopt($ch, CURLOPT_SSLVERSION, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_VERBOSE, true);
$fp = fopen(dirname(__FILE__).'/errorlog.txt', 'w');
curl_setopt($ch, CURLOPT_STDERR, $fp);
// $output contains the output string
$result = curl_exec($ch);
if ($result === FALSE) {
printf("cUrl error (#%d): %s<br>\n", curl_errno($ch),
htmlspecialchars(curl_error($ch)));
}
fclose($fp);
curl_close($ch);
结果是:
cUrl error (#28): Operation timed out after 5000 milliseconds with 0 out of 0 bytes received
错误日志:
* Rebuilt URL to: https://cloudflare.com/
* Trying 198.41.214.162...
* TCP_NODELAY set
* Connected to cloudflare.com (198.41.214.162) port 443 (#0)
* ALPN, offering http/1.1
* Cipher selection: ALL:!EXPORT:!EXPORT40:!EXPORT56:!aNULL:!LOW:!RC4:@STRENGTH
* successfully set certificate verify locations:
* CAfile: /etc/pki/tls/certs/ca-bundle.crt
CApath: none
* Operation timed out after 5000 milliseconds with 0 out of 0 bytes received
* stopped the pause stream!
* Closing connection 0