使用theMovieDB来显示带有PHP的图像海报

时间:2022-11-23 11:04:54

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.

我是使用themoviedb JSON api的新手,目前我正在尝试做一些看似简单的事情:显示电影的主海报。我有一个API键,这是我正在使用的代码/响应。

$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

编辑:添加了配置数组,但是echo什么都没有返回。print fine和print_r的JSON似乎与json_decode兼容,我不知道为什么不能从数组中提取任何值

3 个解决方案

#1


6  

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

为了获得一些额外的信息,您似乎需要对/3/配置做额外的请求。

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

您正在寻找的参数是base_url,您可以使用它来构造url。

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

阅读更多:http://docs.themoviedb.apiary.io/ - % 2 f3 % 2 fconfiguration

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)

你可能想知道为什么要这么做。根据他们的网站,他们这么做是为了保持API的亮度,而且似乎base_url很少变化(更多信息请参见https://www.themoviedb.org/talk/515a72d060ee3615a0b5256)

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。


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

编辑:叹气,我以为您的问题只是因为没有base_url,而不是从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.

就是这样。

#2


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:

你关闭。您必须从配置中获取base_url,就像您对电影查询(http://docs.themoviedb.apiary.io/#configuration)所做的那样,并将base_url前置到您所拥有的内容,然后还需要海报或背景大小。应该是这样的:

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

因此输出应该是这样的:http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w185/2lECpi35Hnbpa4y46JX0aY3AWTy.jpg

#3


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.

如果您的问题是关于如何获取base_url,那么请查看vadderung的答案。

#1


6  

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

为了获得一些额外的信息,您似乎需要对/3/配置做额外的请求。

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

您正在寻找的参数是base_url,您可以使用它来构造url。

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

阅读更多:http://docs.themoviedb.apiary.io/ - % 2 f3 % 2 fconfiguration

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)

你可能想知道为什么要这么做。根据他们的网站,他们这么做是为了保持API的亮度,而且似乎base_url很少变化(更多信息请参见https://www.themoviedb.org/talk/515a72d060ee3615a0b5256)

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。


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

编辑:叹气,我以为您的问题只是因为没有base_url,而不是从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.

就是这样。

#2


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:

你关闭。您必须从配置中获取base_url,就像您对电影查询(http://docs.themoviedb.apiary.io/#configuration)所做的那样,并将base_url前置到您所拥有的内容,然后还需要海报或背景大小。应该是这样的:

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

因此输出应该是这样的:http://d3gtl9l2a4fn1j.cloudfront.net/t/p/w185/2lECpi35Hnbpa4y46JX0aY3AWTy.jpg

#3


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.

如果您的问题是关于如何获取base_url,那么请查看vadderung的答案。