Der Code
Ich habe eine PHP-Klasse erstellt, um mit der API von Instagram zu kommunizieren. Ich verwende eine private Funktion namens api_request
(siehe unten), um mit der API von Instagram zu kommunizieren:
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;
}
}
Diese beiden Codezeilen…
$this->data["pagination"] = $response["pagination"]["next_url"];
$this->data["response"] = $response["data"];
… richtet meine Daten in diesem Array ein:
private $data = array (
"response" => null,
"pagination" => null,
"photos" => array()
);
Das Problem
Immer wenn ich die nächste Seite anfordere, mit folgender Funktion:
public function pagination_query() {
$this->api_request( $this->data["pagination"] );
$output = json_encode( $this->data["photos"] );
return $output;
}
Instagram gibt mir die erste Seite, immer und immer wieder Gewinn. Irgendeine Idee, was hier das Problem ist?
Update Nr. 1
Ich habe das gemerkt, weil meine setup_data
Funktion (verwendet in api_request
Funktion) schiebt meine Fotoobjekte an das Ende meiner $this->data["photos"]
Array:
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 );
}
}
…es ist notwendig, ein leeres Array zu erstellen, wenn eine neue Seite angefordert wird:
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;
}
Ich kann die zweite Seite abrufen, aber alle nachfolgenden pagination_query
Aufrufe geben nur die zweite Seite zurück. Irgendwelche Ideen, was falsch sein könnte?
Update Nr. 2
Ich habe das mit a entdeckt while
Aussage, um die zu machen api_request
Der Funktionsaufruf selbst ermöglicht es mir, Seite für Seite problemlos abzurufen:
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;
}
}
Dies behebt jedoch nicht meine pagination_query
Funktion und es scheint, als ob meine try-catch
Block erstellt eine Schließung und ich bin mir nicht sicher, was ich davon halten soll.
was ist die ausgabe von
$response
Reihe– Michael St. Clair
14. Juni 2015 um 14:31 Uhr
Hallo @MichaelStClair, die Struktur des Antwortumschlags findest du hier: Instagram-API-Endpunkte
– Wilhelm
14. Juni 2015 um 14:41 Uhr
ausdrucken
$response["pagination"]["next_url"];
und sehen, ob es überhaupt einen Wert gibt– Michael St. Clair
14. Juni 2015 um 14:44 Uhr
@MichaelStClair Das habe ich auch vermutet, aber es gibt wie erwartet den Parameter pagination.next_url aus dem json zurück.
– Wilhelm
14. Juni 2015 um 14:53 Uhr
brauchst du die
$this->
davor, um diesen Wert dem Array zuzuweisen? Was passiert, wenn du das entfernst?– Michael St. Clair
14. Juni 2015 um 14:56 Uhr