HTTP/1.1 505 HTTP版本不受支持

10

我正在运行一个PHP脚本来获取网页。它可以很好地处理许多网站,但是在某个网站上失败了,返回一个错误,指出“HTTP/1.1 505 HTTP版本不受支持”。

这是我的脚本的一部分:

for($i = 0; $i < 1; $i++) {
    $page = file_get_contents("http://www.lovelybooks.de/stoebern/empfehlung/romantic fantasy/?seite=$i");
    // do something with $page
}

很多答案建议明确设置HTTP版本。我已经尝试设置0.9、1.0和1.1,但没有改变任何东西。实际上,标头似乎显示我的浏览器请求的HTTP版本与服务器期望的版本相匹配:

回复标头:

HTTP/1.1 200 OK
Date: Mon, 15 Dec 2014 09:01:15 GMT
Server: Apache
X-Powered-By: PHP/5.4.35
Keep-Alive: timeout=2, max=200
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html

请求头:

GET /path/script.php HTTP/1.1
Host: www.mydoman.de
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:34.0) Gecko/20100101 Firefox/34.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: de,en-US;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate
DNT: 1
Authorization: Basic MjQwMjE5Njc6MDcwNjIwMDc=
Connection: keep-alive
Cache-Control: max-age=0

还可能有什么问题?

2个回答

15

使用URL百分比编码替换URL中的空格:

    $page = file_get_contents("http://www.lovelybooks.de/stoebern/empfehlung/romantic%20fantasy/?seite=$i");

谢谢,我已经看到了(请参考我对其他答案的评论)。我真是太蠢了。 - user1322720

1

我看到你在URL中使用了空格。这样是行不通的。为了解决问题,我会将URL放入另一个变量中并进行编码,如下所示:

$URL = urlencode("http://www.lovelybooks.de/stoebern/empfehlung/romantic fantasy/?seite=".$i);

file_get_contents($URL);

1
不要对整个内容进行编码。这将编码“://”的部分,从而防止PHP解析它。 - Barmar
哈哈,谢谢。我知道URL编码可能是个问题,但是没有看到URL中的空格,因为它被我的编辑器窗口宽度所隐藏。我只需在那个空格处放置"%20"即可解决问题。 - user1322720

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接