source

왜 Instagram의 페이지 수가 같은 페이지를 반복해서 반환하는 걸까요?

lovecheck 2023. 3. 28. 21:54
반응형

왜 Instagram의 페이지 수가 같은 페이지를 반복해서 반환하는 걸까요?

코드

Instagram의 API와 통신하기 위해 PHP 클래스를 만들었습니다.개인 기능을 사용하고 있습니다.api_requestInstagram 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_requestFunction Call 자체에서 페이지를 차례로 불러올 수 있습니다.

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블록이 폐쇄를 만들고 있는데 어떻게 해야 할지 모르겠어요.

첫 번째 코드 조각에는 다음과 같은 내용이 있습니다.

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

2개의 키에 주의해 주세요.response그리고.next_page

다음으로 두 번째 스니펫에는 다음과 같은 내용이 있습니다.

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

지금이다next_pagepagination

를 사용하면

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

하지만 당신은$this->data["next_page"] = $response["pagination"]["next_url"];물론 당신은 올바른 결과를 얻지 못할 것이다.

어떤 경우에도 $request의 내용을 var_dump해 보십시오.

public function pagination_query() {
    var_dump($this->data["pagination"]);
    $this->api_request( $this->data["pagination"] );
    $output = json_encode( $this->data["photos"] );

    return $output;
}

디버거가 있는 경우 매번 전달되는 URL인 api_request 내부도 체크합니다.

다음 코드 행을 추가했습니다.try나의 블록try...catch한 번에 여러 페이지를 연속해서 반환하는 문:

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

이것은 본질적으로 말한다.api_request내가 할 때까지 스스로를 칭하는 것$this->data["next_page"]어레이에는 160개의 "그램"이 장착되어 있습니다.

Instagram의 api에서 총 320g을 검색할 수 있습니다.api_request페이지 로드 후 호출되며 첫 번째 페이지 로드 후 160이 호출됩니다.pagination_query불러.

이것은 세컨드로서 이상적인 솔루션이 아닙니다.pagination_query은 첫 번째 콜과 동일한 데이터를 반환한다.pagination_query일단은 해야 할 것 같아요.

갱신하다

위의 해결책은 해킹입니다.누군가 왜 내가 내 친구인지 알아낼 수 있다면pagination_query아직 방법이 효과가 없습니다.감사합니다.

언급URL : https://stackoverflow.com/questions/30756950/why-does-instagrams-pagination-return-the-same-page-over-and-over

반응형