为什么instagram的分页一遍又一遍地返回相同的页面?


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

代码

我已经创建了一个PHP类与Instagram的API通信。我正在使用一个名为api_request的私人函数(如下所示)与Instagram的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;
    }
}

这两行代码…

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

…在这个数组中设置我的数据:

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

每当我请求下一页时,使用以下函数:

public function pagination_query() {
    $this->api_request( $this->data["pagination"] );
    $output = json_encode( $this->data["photos"] );
    return $output;
}
Instagram给了我第一页,一遍又一遍。知道问题出在哪里吗?

Update # 1

我意识到,因为我的setup_data函数(在api_request函数中使用)将我的照片对象推到我的$this->data["photos"]数组的末尾:

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 );
    }
}

…当请求一个新页面时,必须创建一个空数组:

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;
}

我能够检索第二页,但所有随后的pagination_query调用只返回第二页。你知道有什么问题吗?

更新# 2

我发现使用while语句,使api_request函数调用本身,允许我检索一页又一页很好:

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;
    }
}

然而,这并没有修复我的pagination_query函数,它看起来好像我的try-catch块正在创建一个闭包,我真的不知道该怎么做。

在你的第一个代码片段中,你有:

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

注意两个键:responsenext_page

然后在第二个代码片段中:

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

现在next_pagepagination

现在如果你使用

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

但是你正在使用$this->data["next_page"] = $response["pagination"]["next_url"];,当然你不会得到正确的结果。

在任何情况下尝试var_dump $request的内容:

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

如果你有一个调试器也检查内部api_request是每次传递的url

我在try...catch语句的try块中添加了以下代码行,以一次返回几个连续的页面:

while ( count( $this->data["photos"] ) < 160 ) {
    $this-> api_request( $this->data["next_page"] );
}

这实际上告诉api_request调用自己,直到我的$this->data["next_page"]数组填充了160 "g。"

这允许我从Instagram的api中检索总共320克:当页面加载后调用api_request时160克,另一个160克来自我的第一个pagination_query调用。

这不是一个理想的解决方案,因为第二个pagination_query调用仍然返回与我的第一个pagination_query调用相同的数据,但它必须暂时这样做。

上面的解决方案是一个hack。如果有人能弄清楚为什么我的pagination_query方法仍然不起作用,那将是非常感激的。