-1

This is the error, i am getting

Warning: file_get_contents(https://api.themoviedb.org/3/movie/39740?api_key=522cec78237f49axxxxxxxxxxx6d1e0c834a): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found

Now, you may founder, what is inside that link?

That page contain this line only

{"status_code":34,"status_message":"The resource you requested could not be found."}

So, I think this is a valid page (which i can open in browser). I just want PHP to stop giving this error, if it failed to open stream.

This is my JSON code

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

Edit: It is not duplicate of that question. That question is related to email and password, where mine is not

Josh Poor
  • 505
  • 1
  • 5
  • 12
  • https://stackoverflow.com/questions/3566487/file-get-contents-failed-to-open-stream-unauthorized – Praveen Kumar Jul 08 '17 at 12:16
  • Possible duplicate of [File-get-contents failed to open stream Unauthorized](https://stackoverflow.com/questions/3566487/file-get-contents-failed-to-open-stream-unauthorized) – Praveen Kumar Jul 08 '17 at 12:16
  • is it really a 404 error? What do you get when you open the link in your browser? – Ivo P Jul 08 '17 at 12:44
  • Not 404, I just get that line, i mentioned in the question @IvoP – Josh Poor Jul 08 '17 at 13:26
  • it is an api. I think of REST. Consider using PHP's curl functions to retrieve the results from that url. You can then check the response headers and act on that. – Ivo P Jul 08 '17 at 13:45

2 Answers2

1

try coreect or incorrect option if (response === "correct") { }

sekaraja
  • 177
  • 1
  • 1
  • 8
  • first check this condition if($content === FALSE) { }. if still you get same problem use @ in front of the call to file_get_contents(): $content = @file_get_contents($site); – sekaraja Jul 08 '17 at 13:40
0

Try to use curl to get the http response code and act when it's 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);
?>
Ivo P
  • 1,722
  • 1
  • 7
  • 18