显示来自IMDB API和json_decode函数的结果

时间:2013-09-15 07:22:01

标签: php json api encode imdb

我刚刚找到了一个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图表?我在谷歌和stackoverflow搜索,但它是如此努力!

1 个答案:

答案 0 :(得分: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$

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