Bộ luật
tôi đã tạo ra một lớp PHP để giao tiếp với API Instagram của. Tôi đang sử dụng một chức năng riêng được gọi là api_request
(hình dưới đây) để giao tiếp với API Instagram của:Tại sao số trang của Instagram trả lại cùng một trang?
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;
}
}
Hai dòng mã ...
$this->data["pagination"] = $response["pagination"]["next_url"];
$this->data["response"] = $response["data"];
... thiết lập dữ liệu của tôi trong mảng này :
private $data = array (
"response" => null,
"pagination" => null,
"photos" => array()
);
vấn đề
Bất cứ khi nào tôi yêu cầu trang tiếp theo, với chức năng sau:
public function pagination_query() {
$this->api_request($this->data["pagination"]);
$output = json_encode($this->data["photos"]);
return $output;
}
Instagram cho tôi trang đầu tiên, hết lần này đến lần khác. Bất kỳ ý tưởng nào là vấn đề ở đây?
Update # 1
tôi nhận ra rằng vì setup_data
chức năng của tôi (được sử dụng trong api_request
chức năng) đẩy đối tượng hình ảnh của tôi vào cuối $this->data["photos"]
mảng của tôi:
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);
}
}
... nó là cần thiết để tạo một mảng trống, khi yêu cầu một trang mới:
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;
}
Tôi có thể truy xuất trang thứ hai nhưng tất cả các cuộc gọi pagination_query
tiếp theo chỉ trả lại trang thứ hai. Bất kì ý kiến nào có thể sai?
Update # 2
tôi phát hiện ra rằng việc sử dụng một tuyên bố while
, để làm cho api_request
chức năng gọi chính nó, cho phép tôi lấy trang sau khi trang tốt:
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;
}
}
Tuy nhiên, doesn này 't sửa chức năng của tôi pagination_query
và nó xuất hiện như thể khối try-catch
của tôi là tạo ra một đóng cửa và tôi không thực sự chắc chắn những gì để làm cho điều này.
đầu ra của mảng '$ response' là gì –
Hi @MichaelStClair bạn có thể tìm thấy cấu trúc của phong bì phản hồi tại đây: [Instagram API Endpoints] (https://instagram.com/developer/endpoints/) – Wilhelm
in ra '$ response [" pagination "] [" next_url "];' và xem liệu có một giá trị ở đó không –