php HTTP 请求失败!HTTP/1.1 505 HTTP 版本不支持错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2757404/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:35:07  来源:igfitidea点击:

HTTP request failed! HTTP/1.1 505 HTTP Version Not Supported error

phphttpcurlfile-get-contents

提问by shyam

I'm trying to use file_get_contents()to get the response from a server and this error was encountered. Could someone tell me what is the reason and how to fix it? The portion of the code is:

我正在尝试使用file_get_contents()从服务器获取响应,但遇到了此错误。有人能告诉我是什么原因以及如何解决吗?代码部分是:

$api = "http://smpp5.routesms.com:8080/bulksms/sendsms?username=$username&password=$password&source=$source&destination=$destin&dlr=$dlr&type=$type&message=$message";
$resp = file_get_contents($api);

The server responded correctly while I pasted the url in the browser. I learned that this is caused by the server rejecting the client's HTTP version, but I have no idea why that is happening in my case.

当我在浏览器中粘贴 url 时,服务器响应正确。我了解到这是由服务器拒绝客户端的 HTTP 版本引起的,但我不知道为什么会在我的情况下发生这种情况。

Any help is much appreciated. Thanks in advance

任何帮助深表感谢。提前致谢

回答by shyam

I found the problem, and it was a simple coding error -- missing url encoding.

我发现了问题,这是一个简单的编码错误——缺少 url 编码。

The reason I didn't notice it at first was because the code was ok before I did some editing, and I'd missed out the urlencode()function before calling the server, which caused a space in the url.

一开始没注意到的原因是因为在编辑之前代码没问题,urlencode()在调用服务器之前漏掉了这个函数,导致url有空格。

This does seem to be the reason this error occurs for most people. So if you encounter this, use urlencode()on all variables which may contain white space in it's value used as URL parameters. So in the case in my question the fixed code will look like:

这似乎是大多数人发生此错误的原因。因此,如果您遇到这种情况,请urlencode()在用作 URL 参数的值中可能包含空格的所有变量上使用。因此,在我的问题中,固定代码如下所示:

$api = "http://smpp5.routesms.com:8080/bulksms/sendsms?username=$username&password=$password&source=$source&destination=$destin&dlr=$dlr&type=$type&message=" . urlencode($message);
$resp = file_get_contents($api);

Also, thanks for all of your time and responses, those were informational.

另外,感谢您的所有时间和回复,这些都是信息性的。

回答by Gumbo

You could create a stream contextwith the HTTP version set to 1.0 and use that context with file_get_contents:

您可以创建一个HTTP 版本设置为 1.0的流上下文,并将该上下文用于file_get_contents

$options = array(
    'http' => array(
        'protocol_version' => '1.0',
        'method' => 'GET'
    )
);
$context = stream_context_create($options);
$api = "http://smpp5.routesms.com:8080/bulksms/sendsms?username=$username&password=$password&source=$source&destination=$destin&dlr=$dlr&type=$type&message=$message";
$resp = file_get_contents($api, false, $context);

By the way: Don't forget to escape your URI argument values properly with urlencode.

顺便说一句:不要忘记使用urlencode.

回答by therealklanni

I ran into the same issue and in my case the culprit was an errant newline/CRLF character at the end of the request URL, which does not get caught by urlencode()(or maybe it does encode it but it still causes the server to produce the error). Once I found the problem the requests began to work again, even without the stream context options.

我遇到了同样的问题,在我的情况下,罪魁祸首是请求 URL 末尾的错误换行符/CRLF 字符,它没有被捕获urlencode()(或者它确实对其进行了编码,但它仍然导致服务器产生错误)。一旦我发现问题,即使没有流上下文选项,请求也会再次开始工作。

Hopefully this will help others.

希望这会帮助其他人。

回答by LIGHT

Some time we still get error with

有时我们仍然会出错

file_get_contents($api);

in that case, try this:

在这种情况下,试试这个:

fopen($api,"r");

回答by kanchan

I was also facing this same issue.. later i found that while retrieving the results from mysql, Limit $count , $count was -ve. fixing that the url worked fine. There is some problem in url only, and its not a file_get_contents or http version issue..

我也面临同样的问题..后来我发现从 mysql 检索结果时,限制 $count , $count 是 -ve。修复 url 工作正常。仅在 url 中存在一些问题,而不是 file_get_contents 或 http 版本问题..

回答by Mark Nottingham

Can you sniff what's happening on the wire? Seeing the format of the HTTP request as it goes out on the wire would help a lot.

你能嗅出电线上发生了什么吗?当 HTTP 请求通过网络发出时,查看它的格式会有很大帮助。

Without seeing that, my best guess would be that the server isn't well-implemented, and is rejecting a HTTP/1.1 request. Try setting --http1.0on Curl and seeing what happens...

如果没有看到,我最好的猜测是服务器没有很好地实现,并且拒绝了 HTTP/1.1 请求。尝试--http1.0在 Curl 上设置,看看会发生什么......