file_get_contents 抛出 400 Bad Request 错误 PHP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8482782/
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
file_get_contents throws 400 Bad Request error PHP
提问by Javier Villanueva
I'm just using a file_get_contents()
to get the latest tweets from a user like this:
我只是使用 afile_get_contents()
来从这样的用户那里获取最新的推文:
$tweet = json_decode(file_get_contents('http://api.twitter.com/1/statuses/user_timeline/User.json'));
This works fine on my localhost but when I upload it to my server it throws this error:
这在我的本地主机上运行良好,但是当我将其上传到我的服务器时,它会引发此错误:
Warning:file_get_contents(http://api.twitter.com/1/statuses/user_timeline/User.json) [function.file-get-contents]:failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request...
警告:file_get_contents( http://api.twitter.com/1/statuses/user_timeline/User.json) [function.file-get-contents]:无法打开流:HTTP 请求失败!HTTP/1.0 400 错误请求...
Not sure what might be causing it, maybe a php configuration I need to set on my server?
不确定是什么原因造成的,也许我需要在我的服务器上设置一个 php 配置?
Thanks in advance!
提前致谢!
采纳答案by Ben Rowe
You might want to try using curl to retrieve the data instead of file_get_contents. curl has better support for error handling:
您可能想尝试使用 curl 而不是 file_get_contents 来检索数据。curl 对错误处理有更好的支持:
// make request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://api.twitter.com/1/statuses/user_timeline/User.json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
// convert response
$output = json_decode($output);
// handle error; error output
if(curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 200) {
var_dump($output);
}
curl_close($ch);
This may give you a better idea why you're receiving the error. A common error is hitting the rate limit on your server.
这可能会让您更好地了解为什么会收到错误消息。一个常见的错误是达到服务器的速率限制。
回答by Wandeber
You can use file_get_contents
adding the ignore_errors
option set to true
, in this way you will get the entire body of the response in case of error (HTTP/1.1 400, for example) and not only a simple false
.
您可以使用file_get_contents
将ignore_errors
选项设置为true
,这样您将在出现错误(例如 HTTP/1.1 400)时获得整个响应主体,而不仅仅是一个简单的false
.
You can see an example here: https://stackoverflow.com/a/11479968/3926617
你可以在这里看到一个例子:https: //stackoverflow.com/a/11479968/3926617
If you want access to response's headers, you can use $http_response_header
after the request.
如果要访问响应的标头,可以$http_response_header
在请求之后使用。
http://php.net/manual/en/reserved.variables.httpresponseheader.php
http://php.net/manual/en/reserved.variables.httpresponseheader.php
回答by bytepan
Just a little addendum on Ben's answer. According to the PHP manualthe CURLOPT_URL option may be set when inizializing the cURL handle with curl_init().
只是 Ben 回答的一点附录。根据PHP 手册,可以在使用 curl_init() 初始化 cURL 句柄时设置 CURLOPT_URL 选项。
// make request
$ch = curl_init("http://api.twitter.com/1/statuses/user_timeline/User.json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
// convert response
$output = json_decode($output);
// handle error; error output
if(curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 200) {
var_dump($output);
}
curl_close($ch);