使用theMovieDB显示图像海报用PHP

人气:2,085 发布:2022-09-15 标签: php api json themoviedb-api

问题描述

我是新来使用themoviedb JSON API,和我目前正在做一些事情,似乎很简单:显示电影的主海报。我得到了一个API密钥,这里是code /响应我使用。

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'] . "'/>");

我现在的唯一的问题是我想呼应标签显示的海报,但我不知道什么是正确的code是它会是这样的?

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'] . "'/>");

任何帮助将是AP preciated!

Any help would be appreciated!

编辑:添加了配置阵列,但什么也没有回声的回报。两者的JSON打印出罚款和的print_r 似乎与 json_de code 来上班,我只是不知道我为什么不能从阵列拉出所有值

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 /配置为了得到一些额外的信息。

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

您正在寻找的参数是 BASE_URL ,你可以用它来构建的URL。

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

在这里阅读更多:http://docs.themoviedb.apiary.io/#get-%2F3%2Fconfiguration

您可能想知道这是为什么必需的。根据他们的网站,他们做到了,使他们能够保持他们的API光,似乎 BASE_URL 很少改变(见https://www.themoviedb.org/talk/515a72d0760ee3615a0b5256更多)

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)

所以,你能做到这一点的要求只有一次每隔X时间(像曾经例如一天),然后使用 BASE_URL 我在所有的后续请求那里我可能需要做。

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.

编辑:唉,我还以为你有问题,只对不具有 BASE_URL ,还未得到来自JSON数据

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

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

这就是它。

749