0

I'm using a movie api(https://www.themoviedb.org/documentation/api), the api only gives me 20 results per page, i want to get 60, so i make 3 requests for 3 pages. I dont know if i have to use json encode or decode the response but this works for the first result list:

$url1 = "http://api.themoviedb.org/3/movie/upcoming?api_key=###&page=1&language=de";
    $url2 = "http://api.themoviedb.org/3/movie/upcoming?api_key=###&page=2&language=de";
        $url3 = "http://api.themoviedb.org/3/movie/upcoming?api_key=###&page=3&language=de";

$response1 = file_get_contents($url1);
$response2 = file_get_contents($url2);
$response3 = file_get_contents($url3);

echo $response1;

I dont know how to put the results of the second and third request into 'results':

enter image description here

angular part

a.filme = []; //declare an empty array

            $http({
              method: 'GET',
              url: 'data/call-api.php'
            }).then(function (response){
                a.filme = response.data.results;
                console.log(response.data);
            },function (error){
                console.log("JSON konnte nicht geladen werden: " + error.status + error.statusText);
            });

the reponse in the browser of the api (http://api.themoviedb.org/3/movie/upcoming?api_key=###&page=1&language=de) looks like this: enter image description here

Thanks in advance :)

2 Answers 2

1

Assuming you only need the results.

$url1 = "http://api.themoviedb.org/3/movie/upcoming?api_key=###&page=1&language=de";
$url2 = "http://api.themoviedb.org/3/movie/upcoming?api_key=###&page=2&language=de";
$url3 = "http://api.themoviedb.org/3/movie/upcoming?api_key=###&page=3&language=de";

//extract results from each response and merge to single array
$response1 = json_decode(file_get_contents($url1),true)["results"];
$response2 = json_decode(file_get_contents($url2),true)["results"];
$response3 = json_decode(file_get_contents($url3),true)["results"];

echo json_encode(array_merge($response1,$response2,$response3));
Sign up to request clarification or add additional context in comments.

Comments

0

If you are only after the results list you can use:

$apiKey = "###";

$out = [];
for ($i = 1; $i <= 3; $i++) {
    $url = "http://api.themoviedb.org/3/movie/upcoming?api_key=".$apiKey."&page=".$i."&language=de";
    $response = file_get_contents($url);

    if (is_array($response->results)) {
        $out = array_merge($out, $response->results);
    }
}

echo json_encode($out);

In angular just use console.log(response);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.