简体   繁体   English

使用PHP的Etsy API分页

[英]Etsy API Pagination with PHP

So, I'm fairly new to working with APIs, and am just trying to play around with using the Etsy API to display a certain set of listings. 因此,我对使用API​​还是比较陌生,只是尝试使用Etsy API来显示一组列表。

By default, the API returns a set of 25 results, and can do a max of 100 at a time. 默认情况下,API返回一组25个结果,一次最多可以执行100个结果。 I'd like to show more than that, so I'm trying to add pagination to my call. 我想展示更多,所以我试图在通话中增加分页。 Here's what I've got so far: 到目前为止,这是我得到的:

<?php

//setting API key

define("API_KEY", XXX);

//setting request url

$url = "https://openapi.etsy.com/v2/listings/active?keywords=unicorn,unicorns&includes=Images:1:0&api_key=" . API_KEY;

while (isset($url) && $url != '') {

        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $response_body=curl_exec($curl);
        curl_close($curl);

        $response = json_decode($response_body);

        foreach ($response->results as $listing) {
                echo "<li>" . $listing->title . " ~*~ " . $listing->price . " " . $listing->currency_code . " ~*~ " . '<a href="' . $listing->url . '" target="_blank">View on Etsy!</a>' . "</li>" . "<br>";
        }

        $url = $response->pagination->next_page;
}

?>

I thought this would loop through and return the next set of 25 results, but it didn't. 我以为这会循环并返回下一组25个结果,但事实并非如此。 Anyone have experience working with this? 任何人都有与此相关的经验吗? Is there somewhere I'm getting tripped up? 有我要绊倒的地方吗?

Thanks! 谢谢!

In your while block, you are assigning the value of the next_page property to $url . while块中,您将next_page属性的值分配给$url
But the actual value is an int , 2, not an url. 但是实际值是int ,2而不是url。
Instead append the next_page to the initial url, as a query string variable. 而是将next_page作为查询字符串变量附加到初始URL。

$url .= "&page=" . $response->pagination->next_page;

See below an example on how you can isolate each process to a function. 参见下面的示例,了解如何将每个进程隔离到一个函数中。

We move the curl operation into its own function where it returns an object from json_decode . 我们将curl操作移到其自己的函数中,该函数从json_decode返回一个对象。

We move the whole processing of the listings into a separate function, where for now, it just prints out the listings. 我们将清单的整个处理移到一个单独的函数中,现在,它只是打印出清单。

This second function is recursive, meaning, that if the next page exists it will call the first function, get the response, then process it. 第二个函数是递归的,这意味着,如果存在下一页,它将调用第一个函数,获取响应,然后对其进行处理。

<?php
//setting API key

define("API_KEY", 'br4j52uzdtlcpp6qxb6we3ge');

function get_listings($page=1){

    $url = "https://openapi.etsy.com/v2/listings/active?keywords=unicorn,unicorns&includes=Images:1:0&api_key=" . API_KEY;

    $url .= "&page=$page";

    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $response_body=curl_exec($curl);
    curl_close($curl);

    $responseObject = json_decode($response_body);
    return $responseObject;
}

function process_listings($responseObject){

    foreach ($responseObject->results as $listing) {
        echo "Title: " . $listing->title . PHP_EOL . 
            "Price " . $listing->price . PHP_EOL . 
            "Currency code " . $listing->currency_code . PHP_EOL . 
            'URL ' . $listing->url . PHP_EOL;
    }

    print PHP_EOL . "Pagination " . $responseObject->pagination->next_page . PHP_EOL;
    $next_page = $responseObject->pagination->next_page;
    if ($next_page) {
        $nextPageResponse = get_listings($next_page);
        process_listings($nextPageResponse);
    }
}

$firstPage = get_listings(); // page 1 is default
process_listings($firstPage);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM