Php 针对API连接错误进行验证

Php 针对API连接错误进行验证,php,api,Php,Api,我有一个连接到API的代码,如下所示: $response = file_get_contents($url . $guid . $api_key); $response = json_decode($api_user); 但有时API不可用,scrip会抛出错误。如何验证以检查请求是否成功 我试过: if($response = json_decode(file_get_contents($url . $guid . $api_key))) { // success } 但我仍然收到警

我有一个连接到API的代码,如下所示:

$response = file_get_contents($url . $guid . $api_key);
$response = json_decode($api_user);
但有时API不可用,scrip会抛出错误。如何验证以检查请求是否成功

我试过:

if($response = json_decode(file_get_contents($url . $guid . $api_key))) {
  // success
}
但我仍然收到警告:文件\u获取\u内容…无法打开流:HTTP请求失败错误


URL、GUID和APIKEY都是正确的,并且通常工作正常。但如果没有,我只会得到错误报告。

关闭生产环境的错误报告。file_get_contents应该仍然返回false,并且您应该能够检查它,但是关闭reposrting只会隐藏错误正在发生的事实。必须有一种方法来验证,这样错误就不会在一开始就发生。确实有-如前所述,检查文件\u get\u内容的结果是什么。如果为false,则调用失败。