HTTP/1.1 505 HTTP Version Not Supported

2019-04-05 08:09发布

问题:

I'm running a PHP script to grab a web page. It worked fine with many sites, but with one site it fails, returning an error saying "HTTP/1.1 505 HTTP Version Not Supported".

This is (part of) my script:

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

Many answers recommend setting the HTTP version explicity. I have tried setting 0.9, 1.0 and 1.1, but it did not change anything. And actually the headers seems to show that the HTTP version requested by my browser and that expected by the server match:

Reply headers:

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

Request headers:

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

What else can be wrong?

回答1:

Replace the space in the URL with its percent-encoding:

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


回答2:

I see you're using a space in your URL. That won't work To solve the problem I would put the url in another variable and encode it like so:

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

file_get_contents($URL);