cURL HTTP Code is 404


cURL HTTP Code is 404

示例站点: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);

这可能是什么原因?

假设您在PHP中定义了一个有效的url。这可能是因为你的浏览器被设置为通过代理工作,而你的代码中的curl却不是。

设置$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"...

因此,在你的curl代码中,一切似乎都很好。

我进行了以下测试:

$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)

似乎我发现了错误。这些现在是我的选择。completeUrl是预先检查网站是否需要https或http的url。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);`