vendredi 13 avril 2018

How to loop over the pages of API response? Laravel5

I'm calling an API that return events (parties...etc). I'd like to call its first ten pages and have the results put together in one array.

Each response (page) includes a bunch of events.

I have this in my class: private $events = [];

Each time I call a new page, I want the result to be appended to this array so I can return in altogether.

public function events_beta($location): array {

    try {
        for ($i=1; $i <= 10; $i++) { 
            $response  = file_get_contents('http://api.**.com/json/events/search?app_key=**&location='.$location.'&page='.$i.');
            $this->events = array_merge($this->events, json_decode($response)->events->event);
        }
    } catch ( Exceptions $e ) {
        dd($e);
        throw new Exception( 'SDK returned an error: ' . $e->getMessage() );
    }
    return (array) $this->events;
}

My problem is that the same set of events, maybe from the first page or the last page, get repeated over and over like this:

[event_1, event_2, event_3, event_1, event_2, event_3, event_1, event_2, event_3]



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire