Question

I just found a IMDB API:

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

Problem is i can't show the result for IMDB Charts.

by defult API return is like this:

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
                        (
...

how i can show the top 10 movies title and rank IMDB chart with this API? i searched in google and stackoverflow but it is so hard!

Was it helpful?

Solution

As far as i can see, you can call $movies = $imdb->chart_top(); to get the top 250 movies after you initialize the IMDB class as seen in this link file:

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

In return you get an array with 250 objects when the first been the #1 movie. As in this pattern:

$movies[0]->title => "The Godfather"
$movies[0]->year => "1972"
$movies[0]->tconst => "tt0068646"
// ... 
$movies[1]->title => "The Godfather: Part II"
// ...

And if you just want to show the top 10 you can dump it in an HTML table like this:

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

And it will look something like this:

----------------------------------------
| Rank | Movie                         |
----------------------------------------
|  1   | The Godfather                 |
----------------------------------------
|  2   | The Godfather: Part II        |

Update:

If you do wish to use $imdb->boxoffice(); you can use it like this:

$topBox = $imdb->boxoffice()->list; // Top Box Office. An object.
$movies = $topBox->list; //The list of movies. An array.

Now i have the movies, but since it is arranges slightly different (for example title is an object containing properties including the actual title, we will need to do this instead of the above:

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

And it will look something like this:

------------------------------------------------------------------
| Rank | Movie                   | Gross       | Weekend
--------------------------------------------------------------------
|  1   | Riddick                 | 19,030,375$ | 19,030,375$
-------------------------------------------------------------------
|  2   | Lee Daniels' The Butler | 91,403,106$ | 8,401,729$

You can also get the image and the year if you wish. Hope this helps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top