如何停止错误消息,如果JSON文件无法打开流

时间:2017-07-08 12:12:39

标签: php json

这是错误,我正在

  

警告:file_get_contents(https://api.themoviedb.org/3/movie/39740?api_key=522cec78237f49axxxxxxxxxxx6d1e0c834a):无法打开流:HTTP请求失败!找不到HTTP / 1.1 404

现在,您可能是创始人,该链接内部是什么?

该页面仅包含此行

  

{“status_code”:34,“status_message”:“无法找到您请求的资源。”}

所以,我认为这是一个有效的页面(我可以在浏览器中打开)。我只是希望PHP停止提供此错误,如果它无法打开流。

这是我的JSON代码

 $response = file_get_contents("https://api.themoviedb.org/3/movie/".$requestsDone."?api_key=522cec782xxxx6f6d1e0c834a");
if ($response != FALSE) {
    $response = json_decode($response, true);
}

编辑:这不是该问题的重复。这个问题与电子邮件和密码有关,我的不是

2 个答案:

答案 0 :(得分:1)

尝试核心或不正确的选项 if(response ===“correct”){ }

答案 1 :(得分:0)

尝试使用curl获取http响应代码,并在404

时执行操作
<?php
// create a new cURL resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "https://api.themoviedb.org/3/movie/39740?api_key=522cec78237f49axxxxxxxxxxx6d1e0c834a");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$responsejson = curl_exec($ch);
switch ($http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE)) {
   case 200:  # OK
    // do your normal process
    $response = json_decode($responsejson, true);
   break;
  case 404: 
    // do your thing for not found situation
   $response = 'Not found';
  break;
  case 401: 
    // not allowed
   $response = 'api key wrong?';
  break;
 default:
  echo 'Unexpected HTTP code: ', $http_code, "\n";
 }
}


// close cURL resource, and free up system resources
curl_close($ch);
?>