cURL HTTP代码是404

时间:2013-12-11 14:00:05

标签: php curl

示例网站:http://web.de

浏览器中的

:没有任何问题

cURL中的

:找不到错误404。

卷曲选项

$cookie_file_path   = "/adm/cookie.txt";


$header[0]          = "Accept: text/html,application/xhtml+xml,application/xml,";
$header[0]          .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
$header[]           = "Cache-Control: max-age=0";
$header[]           = "Connection: keep-alive";
$header[]           = "Keep-Alive: 300";
$header[]           = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$header[]           = "Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3";
$header[]           = "Pragma: ";

$ch                 = curl_init();
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
curl_setopt ( $ch, CURLOPT_HEADER, 1);
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $header);
curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt ( $ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt ( $ch, CURLOPT_POST, 0);
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_SSLVERSION,3);
curl_setopt ( $ch, CURLOPT_ENCODING, "");
curl_setopt ( $ch, CURLOPT_MAXREDIRS, 10);
curl_setopt ( $ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt ( $ch, CURLOPT_COOKIEJAR, $cookie_file_path);
curl_setopt ( $ch, CURLOPT_NOBODY, 0);


curl_exec($ch);

这可能是什么原因?

3 个答案:

答案 0 :(得分:0)

假设您的PHP中定义了有效的网址。可能是您的浏览器设置为通过代理工作,而代码中的卷曲不是。

设置$ url =“http://web.de”;并运行代码,最后一行更改为var_dump(curl_exec($ ch));结果显示如下:

string(188828) "HTTP/1.1 200 OK\r\nDate: Wed, 11 Dec 2013 14:05:25 GMT\r\nServer: Apache\r\nX-Frame-Options: deny\r\nExpires: Thu, 21 Feb 2013 11:25:14 GMT\r\nPragma: no-cache\r\nCache-Control: private, max-age=0, proxy-revalidate, no-store, no-cache, must-revalidate, public\r\nVary: User-Agent,Accept-Encoding\r\nX-Appserver: hp-webde-bs004\r\nContent-Type: text/html;charset=UTF-8\r\nContent-Encoding: gzip\r\nRTSS: 1-1250-1\r\nContent-Length: 27965\r\nSet-Cookie: SSLB=.1; path=/; domain=.web.de\r\nSet-Cookie: SSID=BgAJEh0O"...

所以你的卷曲代码中的一切似乎都没问题。

答案 1 :(得分:0)

我做了以下测试:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);            
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);    

$raw_data = curl_exec($ch);
curl_close($ch);

var_dump($raw_data);

输出:

string '<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://www.w3.org/1999/xhtml" xml:lang="de" lang="de" data-toolbar-loggedin="false">
<head>
<title>WEB.DE - E-Mail-Adresse kostenlos, FreeMail, De-Mail &amp; Nachrichten</title>
<meta charset="utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
<link rel="start" href="http://web.de/"/>
<link rel="help" href="https://kundenservice.web.de/"/>
<link rel="copyright" href="http://web.de/Impressum/"/>
'... (length=187595)

答案 2 :(得分:0)

好像我发现了错误。这些现在是我的选择。 completeUrl是预先检查网站是否需要https或http的网址。似乎ssl存在一些问题

curl_setopt($ch, CURLOPT_URL, $completeUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, getCustomUserAgent());
curl_setopt($ch, CURLOPT_HEADER, $header);
curl_setopt($ch, CURLOPT_ENCODING, "gzip");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ( $ch, CURLOPT_SSLVERSION,3);
curl_setopt ( $ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt ( $ch, CURLOPT_COOKIEJAR, $cookie_file_path);`