Hello
I am brand new to using the TMDB API.
I am using PHP to search for a movie by text and return the results on a web page.
I can successfully search by using IMDB ID with the following code
$key = "REMOVED"
$query = $_POST["filename"];
$json = file_get_contents("https://api.themoviedb.org/3/movie/$query?api_key=$key");
$result = json_decode($json, true);
$poster_path = $result["poster_path"];
$backdrop_path = $result["backdrop_path"];
$overview = $result["overview"];
echo '
<div id="item" style="width: 90%;">
<div id="title">
'.$result["title"].'
</div>
<div id="right">
<div id="poster" style="float: left; width: 20%;">
<img src="https://image.tmdb.org/t/p/w500'.$poster_path.'" style="width: 100px; height: auto;">
</div>
<div id="overview" style="float: left; width: 80%;">
'.$result["overview"].'
</div>
</div>
</div>
';
}
?>
<div>
<form id="search" method="post" action="ajax_calls.php">
Film: <input type="text" id="filename" name="filename" />
<input type="submit" value="Search" />
</form>
</div>
But when I replace the API URL with:
https://api.themoviedb.org/3/search/movie?api_key=$key&query=Jack+Reacher
It returns no results. Using that URL to directly query the API via a web browser returns an array as expected.
Can anyone point me in the right direction as to where I'm going wrong?
Thanks
Can't find a movie or TV show? Login to create it.
Want to rate or add this item to a list?
Not a member?
Reply by Axiom
on February 11, 2018 at 10:09 PM
That should work to my eyes. Could you upload a full sample of your code to github or somewhere (without your key obviously) so I can see where the issue is?
also to the mods: should I be able to see that form input box? you might want to fix that!
Reply by Axiom
on February 12, 2018 at 1:38 AM
Oh ok now the code markup has been clarified there are a number of mistakes there.
There is no semicolon after $key = "REMOVED"
The api call should be https://api.themoviedb.org/3/search/movie?query= $query&api_key=$key
and there's a trailing curly bracket on line 30 that shouldn't be there.
Edit: It also seems that the file_get_contents PHP function doesn't like ampersands (&), so I suggest using a workaround or simply using curl (Client URL function) instead.