1

I am building a small movie database to learn php. I make a call to the API from The Movie DB:

$url = "https://api.themoviedb.org/3/search/movie?api_key=". $apiKey . "&" . $language . "&query=". $searchTerm ."&". $sortBy .""; // path to your JSON file

The results for the search term brother looks like:

object(stdClass)#2 (4) {
  ["page"]=>
  int(1)
  ["total_results"]=>
  int(1202)
  ["total_pages"]=>
  int(61)
  ["results"]=>
  array(20) {
    [0]=>
    object(stdClass)#3 (14) {
      ["popularity"]=>
      float(18.305)
      ["vote_count"]=>
      int(219)
      ["video"]=>
      bool(false)
      ["poster_path"]=>
      string(32) "/frhzQ5JJ29R3kXuVq3CRYQSczpn.jpg"
      ["id"]=>
      int(327)
      ["adult"]=>
      bool(false)
      ["backdrop_path"]=>
      string(32) "/cL1VX6WllejQQhGTUGGWHW25yor.jpg"
      ["original_language"]=>
      string(2) "en"
      ["original_title"]=>
      string(7) "Brother"
      ["genre_ids"]=>
      array(3) {
        [0]=>
        int(80)
        [1]=>
        int(18)
        [2]=>
        int(53)
      }
      ["title"]=>
      string(7) "Brother"
      ["vote_average"]=>
      int(7)
      ["overview"]=>
      string(613) "Ausgestoßen durch die Bruderschaft seines Yakuza-Clans wird der eiskalte Killer Yamamoto gezwungen, Tokio Richtung Los Angeles zu verlassen. Dort angekommen findet er sich selbst sehr schnell in der alten Routine seines gewalttätigen Tokio Leben wieder. In kürzester Zeit formt er eine neue skrupellos operierende Bruderschaft um sich herum. Macht, Mädchen und Geld liegen im zu Füssen. Aber das ist dem ehemaligen Yakuza Killer Yamamoto nicht genug. Er will alles! Er startet einen blutigen und erbitterten Kampf gegen die Mafia und muss wieder einmal feststellen, dass er von seinen Brüdern verraten wird."

The results show that I am one page 1 of 61 with total results of 1202.

I know that I have to loop through the total_pages but I don't know how to build that query around the API call.

I am thankful for every hint.

hashyx
  • 87
  • 7

1 Answers1

0

From their documentation you can see that there is an optional parameter "page". So you can send following requests for each page like:

$url = "https://api.themoviedb.org/3/search/movie?api_key=". $apiKey . "&" . $language . "&query=". $searchTerm ."&". $sortBy . "&page=" . "$pageNumber";

Bean
  • 1
  • Thanks. But how to setup the foreach? This is what I don't get right. – hashyx Aug 31 '20 at 14:41
  • Are you sure that you will use foreach? There migth be two approaches. First is to render the results from page 1 in a table at your site and when the user clicks on page 2 to send another request to this API for the results of page 2 and so on. The other way is to build a " for ($i = 1; $i <= $total_pages; $i++) {...} " loop and inside the body you can perform requests to the API, where $i will be the page number. – Bean Aug 31 '20 at 14:51
  • Okay, I will try that later. Good idea to save unwanted requests. :) – hashyx Aug 31 '20 at 15:05