繁体   English   中英

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

[英]Show results from IMDB API and json_decode Function

我刚刚找到了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显示十大电影标题和IMDB排行榜? 我在谷歌和stackoverflow搜索,但它是如此困难!

据我所见,您可以调用$movies = $imdb->chart_top(); 初始化IMDB类后,要获得前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"
// ...

如果只想显示前十名,则可以将其转储到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.

现在我有电影了,但是由于它的排列略有不同(例如title是一个包含包含实际标题的属性的对象,因此我们需要这样做而不是上面的方法:

<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$

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM