我正在尝试向Shopify Product API发出多个API请求,以便我可以从商店中提取所有产品。我使用的是通过他们的CLI安装的官方Shopify PHP库。我已经阅读了有关如何获取getNextPageQuery的文档。我已经测试过从下一页提取产品数据。
https://github.com/Shopify/shopify-api-php
但由于某些原因,我无法发出第一个请求,存储结果,然后发出第二个请求,然后返回所有结果。
最终,我想进一步改进这一点,并循环所有getNextPageQuery,直到得到所有结果。但现在我只是测试如何将第一页和第二页的数据结合起来。然而,每次尝试,我都只得到第一组结果。
正如我所说,我做了一个测试,只返回了第二页的结果,这是有效的。基本上,在那个测试中,我只是没有返回第一组结果,我只是使用getNextPageQuery并返回第二组结果-以确保我正确使用了getPageInfo方法。
有人能看看我的代码并告诉我哪里出了问题吗?正如我所说的,最终,我确实需要调整这一点,以从一家商店获得所有产品。理想情况下,将它们存储在一个数组中,并将它们传递回前端。
顺便说一下,由于Shopify的费率限制,我现在使用sleep(2);。我想我们只能每2秒表演一次。如果有更好的方法绕过这一点,请告诉我。
declare(strict_types=1);
namespace App\Lib;
use Shopify\Auth\Session;
use Shopify\Clients\Rest;
class ProductApi {
    public static function getProducts(Session $session, int $limit){
        $response = self::makeApiRequest($session, $limit);
        $productResponseArray = $response->getDecodedBody();
        $pageInfo = $response->getPageInfo();
        if($pageInfo->hasNextPage()){
            sleep(2);
            $secondResponse = self::nextPageApiRequest($session, $pageInfo->getNextPageQuery());
            array_push( $productResponseArray, $secondResponse->getDecodedBody() );
        }
    
        return $productResponseArray;
    }
    private static function makeApiRequest(Session $session, $limit){
        $client = new Rest($session->getShop(), $session->getAccessToken());
        $response = $client->get('products', [], ["limit" => $limit, "fields" => "id, title, options, product_type, tags, variants, image"]);
        return $response;
    }
    private static function nextPageApiRequest(Session $session, $nextPage){
        $client = new Rest($session->getShop(), $session->getAccessToken());
        $response = $client->get('products', [], $nextPage);
        return $response;
    }
}
提前感谢。