Wil*_*elm 12 php oop wordpress pagination 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;
}
}
Run Code Online (Sandbox Code Playgroud)
这两行代码......
$this->data["pagination"] = $response["pagination"]["next_url"];
$this->data["response"] = $response["data"];
Run Code Online (Sandbox Code Playgroud)
...在此数组中设置我的数据:
private $data = array (
"response" => null,
"pagination" => null,
"photos" => array()
);
Run Code Online (Sandbox Code Playgroud)
每当我请求下一页时,使用以下功能:
public function pagination_query() {
$this->api_request( $this->data["pagination"] );
$output = json_encode( $this->data["photos"] );
return $output;
}
Run Code Online (Sandbox Code Playgroud)
Instagram给了我第一页,一遍又一遍.知道这里的问题是什么吗?
我意识到,因为我的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 );
}
}
Run Code Online (Sandbox Code Playgroud)
...在请求新页面时,有必要创建一个空数组:
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;
}
Run Code Online (Sandbox Code Playgroud)
我能够检索第二页但所有后续pagination_query调用只返回第二页.什么想法可能是错的?
我发现使用一个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;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,这并没有修复我的pagination_query功能,看起来好像我的try-catch块正在创建一个闭包,我不知道该怎么做.
在您的第一个代码片段中,您有:
$this->data["response"] = $response["data"];
$this->data["next_page"] = $response["pagination"]["next_url"];
Run Code Online (Sandbox Code Playgroud)
注意两个键:response和next_page
然后在你的第二个片段中你有:
$this->data["pagination"] = $response["pagination"]["next_url"];
$this->data["response"] = $response["data"];
Run Code Online (Sandbox Code Playgroud)
现在next_page是pagination
现在如果你使用
$this->api_request( $this->data["pagination"] );
Run Code Online (Sandbox Code Playgroud)
但你正在使用 $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;
}
Run Code Online (Sandbox Code Playgroud)
如果您有调试器,还请检查 api_request 内部,这是每次传递的 url