我刚刚找到了一个IMDB API:
https://github.com/chrisjp/IMDb-PHP-API
问题是我无法显示IMDB图表的结果。
通过 defult API 返回是这样的:
stdClass Object
(
[date] => 2013-09-08
[list] => stdClass Object
(
[label] => Top Box Office for United States
[list] => Array
(
[0] => stdClass Object
(
[weekend] => stdClass Object
(
[currency] => USD
[amount] => 19030375
)
[title] => stdClass Object
(
[tconst] => tt1411250
[type] => feature
[title] => Riddick
[image] => stdClass Object
(
[width] => 1125
[url] => http://ia.media-imdb.com/images/M/MV5BMTk5NzYwMzQ4MV5BMl5BanBnXkFtZTcwMjE5MTI1OQ@@._V1_.jpg
[height] => 1667
)
[year] => 2013
)
[rank] => 1
[gross] => stdClass Object
(
[currency] => USD
[amount] => 19030375
)
)
[1] => stdClass Object
(
[weekend] => stdClass Object
(
[currency] => USD
[amount] => 8401729
)
[title] => stdClass Object
(
[tconst] => tt1327773
[type] => feature
[title] => Lee Daniels' The Butler
[image] => stdClass Object
(
[width] => 1382
[url] => http://ia.media-imdb.com/images/M/MV5BMjM2NDY3MjkyMF5BMl5BanBnXkFtZTcwMDM5Nzg5OQ@@._V1_.jpg
[height] => 2048
)
[year] => 2013
)
[rank] => 2
[gross] => stdClass Object
(
[currency] => USD
[amount] => 91403106
)
)
[2] => stdClass Object
(
...
如何使用此 API 显示前 10 名电影标题并对 IMDB 图表进行排名? 我在谷歌和堆栈溢出中搜索,但这太难了!
据我所知,您可以在初始化 IMDB 类后调用 $movies = $imdb->chart_top();
获取前 250 部电影,如以下链接文件所示:
https://github.com/chrisjp/IMDb-PHP-API/blob/master/tests/charts.php
作为回报,当第一个是#1电影时,你会得到一个包含250个对象的数组。如以下模式所示:
$movies[0]->title => "The Godfather"
$movies[0]->year => "1972"
$movies[0]->tconst => "tt0068646"
// ...
$movies[1]->title => "The Godfather: Part II"
// ...
如果你只想显示前 10 名,你可以像这样将其转储到 HTML 表中:
<table>
<tr>
<td>Rank</td>
<td>Movie</td>
</tr>
<?php
foreach( $movies as $key=>$movie){
if($key >= 10){
break; // Get out after 10 movies.
}
echo '<tr>';
echo '<td>' . ($key + 1) . '</td>'; // Array keys start from 0.
echo '<td>' . $movie->title . '</td>';
echo '</tr>';
}
?>
</table>
它看起来像这样:
----------------------------------------
| Rank | Movie |
----------------------------------------
| 1 | The Godfather |
----------------------------------------
| 2 | The Godfather: Part II |
更新:
如果您确实希望使用$imdb->boxoffice();
则可以像这样使用它:
$topBox = $imdb->boxoffice()->list; // Top Box Office. An object.
$movies = $topBox->list; //The list of movies. An array.
现在我有了电影,但由于它的排列略有不同(例如标题是一个包含包括实际标题在内的属性的对象,我们需要这样做而不是上述操作:
<table>
<tr>
<td>Rank</td>
<td>Movie</td>
<td>Gross</td>
<td>Weekend</td>
</tr>
<?php
foreach( $movies as $key=>$movie){
if($key >= 10){
break; // Get out after 10 movies.
}
echo '<tr>';
echo '<td>' . $movie->rank . '</td>'; // get from the object the rank.
echo '<td>' . addslashes($movie->title->title) . '</td>'; // get the title object, and extract the title string. Add slashes to escape quotes.
echo '<td>' . number_format($movie->gross->amount) . '$</td>'; // get the gross amount and add ',';
echo '<td>' . number_format($movie->weekend->amount) . '$</td>'; // get the weekend amount and add ',';
echo '</tr>';
}
?>
</table>
它看起来像这样:
------------------------------------------------------------------
| Rank | Movie | Gross | Weekend
--------------------------------------------------------------------
| 1 | Riddick | 19,030,375$ | 19,030,375$
-------------------------------------------------------------------
| 2 | Lee Daniels' The Butler | 91,403,106$ | 8,401,729$
如果您愿意,您还可以获取图像和年份。希望这有帮助