我在 PHP 中使用 curl 已有一段时间了。今天我一直在尝试获取 http://www.webhostingstuff.com/category/Best-Hosting.html我不断收到 http 代码 0,这对我来说是新的。
我设置标题
$s->headers = array(
"User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1",
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Language: en-gb,en;q=0.5",
"Accept-Encoding: gzip, deflate",
"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7",
"Keep-Alive: 115",
"Connection: keep-alive",
"Referer: https://google.com"
);
我有一个 cookie 文件(当脚本完成加载时,里面什么都没有)
这是 curl 函数
function fetch($url, $username='', $data='', $proxy=''){
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
if(isset($proxy)) {
curl_setopt($ch,CURLOPT_TIMEOUT,30);
curl_setopt($ch, CURLOPT_PROXY, $proxy);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
curl_setopt($ch, CURLOPT_PROXYPORT, $proxy);
curl_setopt($ch, CURLOPT_PROXYUSERPWD, 'proxyadmin:parola');
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FRESH_CONNECT,true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 5);
if(!empty($username)) {
curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie/{$username}.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie/{$username}.txt");
}
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
if (is_array($data) && count($data)>0) {
curl_setopt($ch, CURLOPT_POST, true);
$params = http_build_query($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
}
if (is_array($this->headers) && count($this->headers)>0){
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
}
$this->result = curl_exec($ch);
$curl_info = curl_getinfo($ch);
$header_size = $curl_info["header_size"];
$this->headers = substr($this->result, 0, $header_size);
$this->http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$this->error = curl_error($ch);
curl_close($ch);
}
我也尝试过从不同的服务器通过 SSH 执行(以防它的 IP 被阻止)
[brian@ip-184-168-22-244 ~]$ curl -url http://www.webhostingstuff.com/addcomments/5ite.html Enter host password for user 'rl': curl: (7) couldn't connect to host [brian@ip-184-168-22-244 ~]$
我该如何解决?
最佳答案
你的命令
curl -url http://www.webhostingstuff.com/addcomments/5ite.html
应该是:
curl --url http://www.webhostingstuff.com/addcomments/5ite.html
cURL 认为您正在指定 -u 选项,该选项用于指定用户名,因此您收到了错误消息。您需要指定 --url(两个破折号)。
希望至少对调试有帮助。
关于php - 为什么我从 curl 得到 HTTP_CODE 0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6887104/