Using theMovieDB to display Image Poster with PHP

18k Views Asked by At

I'm new to using the themoviedb JSON api, and I'm currently trying to do something that seems simple: Display a movie's main poster. I got an API key and here is the code / response I'm using.

$ca = curl_init();
curl_setopt($ca, CURLOPT_URL, "http://api.themoviedb.org/3/configuration?api_key=MYAPIKEY");
curl_setopt($ca, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ca, CURLOPT_HEADER, FALSE);
curl_setopt($ca, CURLOPT_HTTPHEADER, array("Accept: application/json"));
$response = curl_exec($ca);
curl_close($ca);
//var_dump($response);
$config = json_decode($response, true);
//print_r($config);
//$base = $config['base_url'];
//echo($base);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://api.themoviedb.org/3/search/movie?query=Monsters+University&api_key=MYAPIKEY");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/json"));
$response = curl_exec($ch);
curl_close($ch);
$result = json_decode($response, true);
//print_r($result);
//var_dump($response);
echo("<img src='" . $config[0]['base_url'] . $config[0]['poster_sizes'][2] . $result[0]['poster_path'] . "'/>");

My only question now is I'm trying to echo a tag to display the poster, but I'm not sure what the correct code would be would it be something like this?

$responsePHP = json_decode($response);
echo("<img src='" . $responsePHP['poster_path'] . "'/>");

Any help would be appreciated!

Edit: Added the config array, but it the echo returns with nothing. The JSON for both print out fine and print_r seems to work with json_decode, I just don't know why I can't pull out any values from the arrays

3

There are 3 best solutions below

0
On BEST ANSWER

It seems you need to do an additional request to /3/configuration in order to get some extra info.

The parameter you are looking for is base_url which you can use to construct the urls.

Read more here: http://docs.themoviedb.apiary.io/#get-%2F3%2Fconfiguration

You may be wondering why is that required. According to their site, they did it so they can keep their API light, and it seems the base_url rarely changes (see https://www.themoviedb.org/talk/515a72d0760ee3615a0b5256 for more)

So, you could do that request only once every X time (like once a day for instance) and then use the base_url I get there in all the subsequent requests I may need to do.


EDIT: Sigh, I thought you had problems only for not having the base_url, not with getting the data from the json.

Anyway, just replace the last line to this:

echo("<img src='" . $config['images']['base_url'] . $config['images']['poster_sizes'][2] . $result['results'][0]['poster_path'] . "'/>");

And that's it.

3
On

you're close. you have to grab the base_url from configuration just like you did for the movie query (http://docs.themoviedb.apiary.io/#configuration) and prepend the base_url to what you have and then you also need the poster or backdrop size. it should be something like this:

echo("<img src='" . $config['base_url'] . $config['poster_sizes'][2] . $responsePHP['poster_path'] . "'/>");

so the output should be something like: http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w185/2lECpi35Hnbpa4y46JX0aY3AWTy.jpg

2
On

The following will print out the poster path. I think that's what you want.

$responsePHP = json_decode($response);
$movies = $responsePHP->results;
$firstMovie = $movies[0];
echo $firstMovie->poster_path;

If your question is about how to get the base_url then look at vadderung's answer.