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

shy*_*yam 14 php curl http file-get-contents

我正在尝试使用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);
Run Code Online (Sandbox Code Playgroud)

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

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

shy*_*yam 30

我发现了问题,这是一个简单的编码错误 - 缺少网址编码.

我之前没有注意到它的原因是因为在我进行一些编辑之前代码是正常的,并且urlencode()在调用服务器之前我错过了该函数,这导致了url中的空格.

这似乎是大多数人出现此错误的原因.因此,如果您遇到此问题,请使用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);
Run Code Online (Sandbox Code Playgroud)

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


Gum*_*mbo 9

您可以创建 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);
Run Code Online (Sandbox Code Playgroud)

顺便说一句:不要忘记正确地转义你的URI参数值urlencode.