Karl Husten Karl Husten - 2 years ago 142
JSON Question

PHP Angularjs JSON multiple API Responses

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

Answer Source

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));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download