我有一个代码可以连接到如下所示的 API:
$response = file_get_contents($url . $guid . $api_key);
$response = json_decode($api_user);
但有时 API 不可用,脚本会抛出错误。如何验证以检查请求是否成功?
我努力了:
if($response = json_decode(file_get_contents($url . $guid . $api_key))) {
// success
}
但我仍然收到“警告:file_get_contents...无法打开流:HTTP 请求失败”错误。
URL、GUID 和 APIKEY 是正确的,通常可以正常工作。但是当它不是时,我只会得到错误报告。