当PHP请求API失败时如何处理错误

当PHP请求API失败时如何处理错误,php,api,Php,Api,我对API有以下请求: $url = "{{correctURLishere}}"; $response = json_decode(file_get_contents($url), true); $name = $response["title"]; } This应该从json响应中的键“title”返回值。但是,我得到的错误是 文件获取内容(url):无法打开流:HTTP请求失败! HTTP/1.1402 我知道错误是因为我每天只允许150个请求,并且已经超过了这个数量,但是有人能帮

我对API有以下请求:

$url = "{{correctURLishere}}";
$response = json_decode(file_get_contents($url), true);
$name =  $response["title"];
} 

This应该从json响应中的键“title”返回值。但是,我得到的错误是

文件获取内容(url):无法打开流:HTTP请求失败! HTTP/1.1402

我知道错误是因为我每天只允许150个请求,并且已经超过了这个数量,但是有人能帮我回显“不再允许请求”而不是错误吗

我尝试了以下方法,但无效:

if(json_decode(file_get_contents($url), true)) {
$response = json_decode(file_get_contents($url), true);
$name =  $response["title"];
}

有人能告诉我如何帮助解决这个错误吗?谢谢,例如,尝试使用try-catch部分

try{
    if(json_decode(file_get_contents($url), true)) {
        $response = json_decode(file_get_contents($url), true);
        $name =  $response["title"];
    }
}catch(Exception $e){
    print_r($e);
    echo '<br>';
    echo $e->getMessage();
}
试试看{
if(json解码(文件获取内容($url),true)){
$response=json\u decode(文件获取内容($url),true);
$name=$response[“title”];
}
}捕获(例外$e){
印刷品(港币);;
回声“
”; echo$e->getMessage(); }

请参考:

例如,尝试使用try-catch部分

try{
    if(json_decode(file_get_contents($url), true)) {
        $response = json_decode(file_get_contents($url), true);
        $name =  $response["title"];
    }
}catch(Exception $e){
    print_r($e);
    echo '<br>';
    echo $e->getMessage();
}
试试看{
if(json解码(文件获取内容($url),true)){
$response=json\u decode(文件获取内容($url),true);
$name=$response[“title”];
}
}捕获(例外$e){
印刷品(港币);;
回声“
”; echo$e->getMessage(); }

供您参考:

您是这样想的吗?因为我看不到你提到(当然还有证据)你已经确保允许fopen对URL进行操作。这是否回答了你的问题?这回答了你的问题吗?但是你呢?因为我看不到你提到(当然还有证据)你已经确保允许fopen对URL进行操作。这是否回答了你的问题?这回答了你的问题吗?