php - Why does Instagram's pagination return the same page over and over? -
the code
i've created php class communicate instagram's api. using private function called api_request (shown below) communicate instagram's api:
private function api_request( $request = null ) { if ( is_null( $request ) ) { $request = $this->request["httprequest"]; } $body = wp_remote_retrieve_body( wp_remote_get( $request, array( "timeout" => 10, "content-type" => "application/json" ) ) ); try { $response = json_decode( $body, true ); $this->data["pagination"] = $response["pagination"]["next_url"]; $this->data["response"] = $response["data"]; $this->setup_data( $this->data ); } catch ( exception $ex ) { $this->data = null; } } these 2 lines of code...
$this->data["pagination"] = $response["pagination"]["next_url"]; $this->data["response"] = $response["data"]; ...sets data within array:
private $data = array ( "response" => null, "pagination" => null, "photos" => array() ); the problem
whenever request next page, following function:
public function pagination_query() { $this->api_request( $this->data["pagination"] ); $output = json_encode( $this->data["photos"] ); return $output; } instagram gives me first page, on , on , on gain. idea problem here?
update #1
i realized because setup_data function (used in api_request function) pushes photo objects onto end of $this->data["photos"] array:
private function setup_data( $data ) { foreach ( $data["response"] $obj ) { /* code parses api, goes here */ /* pushes new object onto photo stack */ array_push( $this->data["photos"], $new_obj ); } } ...it necessary create empty array, when requesting new page:
public function pagination_query() { $this->data["photos"] = array(); // kicks out old photo objects $this->api_request( $this->data["pagination"] ); $output = json_encode( $this->data["photos"] ); return $output; } i'm able retrieve second page subsequent pagination_query calls return second page. ideas wrong?
update #2
i discovered using while statement, make api_request function call itself, allows me retrieve page after page fine:
private function api_request( $request = null ) { if ( is_null( $request ) ) { $request = $this->request["httprequest"]; } $body = wp_remote_retrieve_body( wp_remote_get( $request, array( "timeout" => 18, "content-type" => "application/json" ) ) ); try { $response = json_decode( $body, true ); $this->data["response"] = $response["data"]; $this->data["next_page"] = $response["pagination"]["next_url"]; $this->setup_data( $this->data ); // while state returns page after page fine while ( count( $this->data["photos"] ) < 80 ) { $this-> api_request( $this->data["next_page"] ); } } catch ( exception $ex ) { $this->data = null; } } however, doesn't fix pagination_query function , appears if try-catch block creating closure , i'm not sure make of this.
in first snippet of code have:
$this->data["response"] = $response["data"]; $this->data["next_page"] = $response["pagination"]["next_url"]; note 2 keys: response , next_page
then in second snippet have:
$this->data["pagination"] = $response["pagination"]["next_url"]; $this->data["response"] = $response["data"]; now next_page pagination
now if use
$this->api_request( $this->data["pagination"] ); but using $this->data["next_page"] = $response["pagination"]["next_url"]; of course not right results.
in case try var_dump content of of $request:
public function pagination_query() { var_dump($this->data["pagination"]); $this->api_request( $this->data["pagination"] ); $output = json_encode( $this->data["photos"] ); return $output; } if have debbugger check inside api_request url passed each time
Comments
Post a Comment