Using theMovieDB to display Image Poster with PHP

api, json, php, themoviedb-api

Solution

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.

Problem

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

Original source