9

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

chris-l
  • 2,802
  • 1
  • 18
  • 18
Vikram
  • 349
  • 3
  • 7
  • 16

3 Answers3

6

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.

chris-l
  • 2,802
  • 1
  • 18
  • 18
0

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

vadderung
  • 13
  • 1
  • 8
  • Check my edit. Something is still not working with pulling values out from the arrays. – Vikram Aug 16 '13 at 19:25
  • if you uncomment your print_r($config); line, what is displayed? – vadderung Aug 16 '13 at 19:49
  • try to change your "config" section to this (sorry about the formatting): $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FAILONERROR, 1); $results = curl_exec($ch); $headers = curl_getinfo($ch); $error_number = curl_errno($ch); $error_message = curl_error($ch); curl_close($ch); $config = json_decode(($results),true); – vadderung Aug 16 '13 at 19:59
0

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.

Rayhan Muktader
  • 2,038
  • 2
  • 15
  • 32
  • Check my edit, for some reason values are not being pulled out of the arrays when I request them, is there some syntax I'm missing? – Vikram Aug 16 '13 at 19:26
  • @Vikram: You are missing array keys. Here's the link for what you want: [http://pastebin.com/Efp05kUV](http://pastebin.com/Efp05kUV). I still recommend you use object notations like I showed in my original example. – Rayhan Muktader Aug 20 '13 at 20:51