【问题标题】:Why does Instagram's pagination return the same page over and over?为什么 Instagram 的分页会一遍又一遍地返回相同的页面?
【发布时间】:2015-08-25 17:47:33
【问题描述】:

守则

我创建了一个 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 给了我第一页,一次又一次地获得。 知道这里有什么问题吗?

更新 #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 块正在创建一个闭包,我不确定该怎么做。

【问题讨论】:

  • $response 数组的输出是什么
  • 嗨@MichaelStClair,您可以在这里找到响应信封的结构:Instagram API Endpoints
  • 打印出$response["pagination"]["next_url"];,看看那里有没有值
  • @MichaelStClair 我也怀疑过,但它按预期从 json 中返回 pagination.next_url 参数。
  • 你需要在它前面的$this-&gt; 来将该值分配给数组吗?如果删除它会发生什么?

标签: php wordpress oop pagination instagram


【解决方案1】:

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

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

这实质上是告诉api_request 调用自己,直到我的$this-&gt;data["next_page"] 数组填充了160“克”。

这使我可以从 Instagram 的 api 中总共检索 320 克:在页面加载后调用 api_request 时为 160 克,从我的第一次 pagination_query 调用中再检索 160 克。

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

更新

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

【讨论】:

    【解决方案2】:

    在您的第一个 sn-p 代码中,您有:

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

    注意两个键:responsenext_page

    那么在你的第二个 sn-p 中你有:

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

    现在next_pagepagination

    现在如果你使用

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

    但是你使用$this-&gt;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

    【讨论】:

    • 感谢您指出这一点。为了清楚起见,我更新了密钥的名称,但忘记在我的问题中更新它(对此感到抱歉)。关于var dump,我将pagination_query 函数中的return 更改为return $this-&gt;data["next_page"],它一遍又一遍地返回到第二页的链接。我想弄清楚的唯一问题是为什么所有后续的pagination_query 调用都会从第一次调用时返回链接?
    • 您认为在我的try-catch 块内有什么东西会导致变量持续存在或不更新吗?
    • 是的,如果在尝试中出现问题...catch 你将无法正确获取 next_url
    • 必须添加一个url供参考api
    • 嘿@rubenrp81,你能解释一下你的意思吗?
    猜你喜欢
    • 2023-01-20
    • 2017-02-15
    • 1970-01-01
    • 1970-01-01
    • 2020-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多