0

我刚刚找到了一个 IMDB API:

https://github.com/chrisjp/IMDb-PHP-API

问题是我无法显示 IMDB 图表的结果。

默认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 图表?我在谷歌和stackoverflow中搜索过,但太难了!

4

1 回答 1

1

据我所知,在初始化 IMDB 类后,您可以调用$movies = $imdb->chart_top();以获取前 250 部电影,如以下链接文件所示:

https://github.com/chrisjp/IMDb-PHP-API/blob/master/tests/charts.php

作为回报,当第一部电影是第一部电影时,您会得到一个包含 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$

如果您愿意,您还可以获取图像和年份。希望这可以帮助

于 2013-09-15T13:26:04.363 回答