php HTTP 错误 400。请求格式错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18685404/
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
HTTP Error 400. The request is badly formed
提问by Harish Kumar
I am getting the same error for this, please suggest
我遇到了同样的错误,请提出建议
$url="http://domain.com/manage/File Name.xml";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url); // get the url contents
$data = curl_exec($ch); // execute curl request
curl_close($ch);
echo $data;
回答by Deepika Patel
This error come, when your curl url contains white spaces. you have to encode url for remove white space.
当您的 curl url 包含空格时,会出现此错误。您必须对 url 进行编码以删除空格。
$base_url = "http://domain.com/manage/";
$url = "File Name.xml";
$ch = curl_init();
$final_url = $base_url . curl_escape($ch, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url); // get the url contents
$data = curl_exec($ch); // execute curl request
curl_close($ch);
echo $data;
回答by édouard Lopez
As describe in the comment, your URL contains not encoded characters (spaces).
如评论中所述,您的 URL 包含未编码的字符(空格)。
Solution
解决方案
Encode your URLwhen setting CURLOPT_URL
:
设置时对您的 URL进行编码CURLOPT_URL
:
curl_setopt($ch, CURLOPT_URL, urlencode($url));
You could also use curl_escape()
to encode the query string part.
您还可以使用curl_escape()
对查询字符串部分进行编码。
References
参考
回答by tim
回答by Aatif Khan
Answers here suggest using url_encode or curl_escape functions. If you want to be RFC 3986 compliant, use rawurlencode() function instead. This helped with a curl request in PHP 7. Hoep it helps others.
此处的答案建议使用 url_encode 或 curl_escape 函数。如果您想符合 RFC 3986,请改用rawurlencode() 函数。这有助于 PHP 7 中的 curl 请求。希望它可以帮助其他人。