Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Instagram's pagination return the same page over and over?

The Code

I've created a PHP class to communicate with Instagram's API. I am using a private function called api_request (shown below) to communicate with 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 two lines of code...

$this->data["pagination"] = $response["pagination"]["next_url"];
$this->data["response"] = $response["data"];

...sets up my data within this array:

private $data = array (
    "response"      => null,
    "pagination"    => null,
    "photos"        => array()
);

The Problem

Whenever I request the next page, with the following function:

public function pagination_query() {
    $this->api_request( $this->data["pagination"] );
    $output = json_encode( $this->data["photos"] );

    return $output;
}

Instagram gives me the first page, over and over and over gain. Any idea what the problem is here?

Update #1

I realized that because my setup_data function (used in api_request function) pushes my photo objects onto the end of my $this->data["photos"] array:

private function setup_data( $data ) {

    foreach ( $data["response"] as $obj ) {

        /* code that parses the api, goes here */


        /* pushes new object onto photo stack */
        array_push( $this->data["photos"], $new_obj );
    }
}

...it is necessary to create an empty array, when requesting a 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 to retrieve the second page but all subsequent pagination_query calls only return the second page. Any ideas what could be wrong?

Update #2

I discovered that using a while statement, to make the api_request function call itself, allows to me retrieve page after page just 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 just fine
        while ( count( $this->data["photos"] ) < 80 ) {
            $this-> api_request( $this->data["next_page"] );
        }

    } catch ( Exception $ex ) {
        $this->data = null;
    }
}

However, this doesn't fix my pagination_query function and it appears as if my try-catch block is creating a closure and I'm not really sure what to make of this.

like image 924
Wilhelm Avatar asked Jun 10 '15 12:06

Wilhelm


People also ask

Why do my Instagram stories keep repeating?

The first thing you should do when you experience Instagram Stories and posts repeating in your news feed is to check your internet connection. Sometimes, your WiFi connection goes weak despite being connected, or your cellular connection falters unexpectedly. These kinds of internet issues are responsible for a whole lot of trouble on Instagram.

Why is Instagram not responding to my posts?

These kinds of internet issues are responsible for a whole lot of trouble on Instagram. You could try rebooting your router or contacting your ISP for help in this regard. The developers tend to keep updating Instagram every two months or so, with upgraded features and bug fixes.

Why is Instagram reducing the number of recommendation posts?

Mosseri told Newton that Instagram will temporarily reduce the number of recommended posts that users see but didn't specify by how much, in order to get better at ranking and presenting recommendations to users. Once Instagram improves its recommendation algorithm, it will start to grow again, Mosseri says.

What's going on with Instagram's changes?

Instagram head Adam Mosseri says the social network will walk back some recent changes to the app that have led to intense criticism from users. Instagram will phase out a test that turned users' home feeds into a TikTok-like full-screen experience that prioritized video in the coming weeks.


1 Answers

In your first snippet of code you have:

    $this->data["response"] = $response["data"];
    $this->data["next_page"] = $response["pagination"]["next_url"];

Note two keys: response and next_page

Then in your second snippet you have:

    $this->data["pagination"] = $response["pagination"]["next_url"];
    $this->data["response"] = $response["data"];

Now next_page is pagination

Now if you use

$this->api_request( $this->data["pagination"] );

But you are using $this->data["next_page"] = $response["pagination"]["next_url"]; of course you will not get the right results.

In any case try to var_dump the content of of your $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 you have a debbugger also check inside api_request which is the url passed each time

like image 157
dynamic Avatar answered Oct 13 '22 19:10

dynamic