PHP分页页面重复,php – 为什么Instagram的分页一遍又一遍地返回相同的页面?

代码

我创建了一个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 [“照片”]数组的末尾:

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块正在创建一个闭包,而且我并不确定该做什么.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值