Php file_get_contents不返回400状态的响应

Php file_get_contents不返回400状态的响应,php,node.js,api,Php,Node.js,Api,我在NodeJS中有一个API,当出现错误时会返回错误消息,但当我想在php中显示“file\u get\u contents”时,它会返回“null”。什么时候应该是: {" errMsg ":" Do not found information! "," Status ": 400} 我认为您得到null是因为您得到了400状态,我在本地主机上测试了它,得到了以下响应: Warning: file_get_contents(http://repuveconsulta.com:3001/a

我在NodeJS中有一个API,当出现错误时会返回错误消息,但当我想在php中显示“file\u get\u contents”时,它会返回“null”。什么时候应该是:

{" errMsg ":" Do not found information! "," Status ": 400}


我认为您得到null是因为您得到了400状态,我在本地主机上测试了它,得到了以下响应:

Warning: file_get_contents(http://repuveconsulta.com:3001/api/plateinfos/1?plate_number=xx&captcha_code=y78): failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request in C:\wamp64\www\****\controllers\Home.php on line 46
C:\wamp64\www***\controllers\Home.php:47:null

也许您可以使用curl检查响应代码,我不知道是否可以使用file\u get\u内容:

$url = 'http://repuveconsulta.com:3001/api/plateinfos/1?plate_number=xx&captcha_code=y78';

$ch           = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result       = curl_exec($ch);
$status_code  = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

var_dump(json_decode($result, true));
var_dump($status_code);
您还可以添加:

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); // The number of seconds to wait while trying to connect. Use 0 to wait indefinitely.
    curl_setopt($ch, CURLOPT_TIMEOUT, 5); // The maximum number of seconds to allow cURL functions to execute.

对于curl请求,因此当您的服务关闭时,站点将不会挂起,直到出现超时错误

检查浏览器中的url或postman,否则调试您的$\u POST变量数据。当我将相同的值插入到您的代码中时,它将返回
string(51){“errMsg”:“未找到信息!”,“status”:400}“
???所以它看起来是有效的。从PHP测试你说的是第一个还是第二个
var\u dump
?如果是第二个,则提供第一个。
file\u get\u contents的输出,如果响应状态代码指示错误,则返回false。您需要特别指示它在这种情况下仍然返回响应体,方法是在
ignore\u errors
设置为true的上下文中传递。
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); // The number of seconds to wait while trying to connect. Use 0 to wait indefinitely.
    curl_setopt($ch, CURLOPT_TIMEOUT, 5); // The maximum number of seconds to allow cURL functions to execute.