【问题标题】:Only one page of product got with rest admin apirest admin api 只获得了一页产品
【发布时间】:2019-08-06 03:48:26
【问题描述】:

我的开发商店中有很多产品页面。但是当我尝试使用 products.json 获取我的所有产品时,只需要 50 个列表(一页)。有人可以解决我的疑问吗?

<?php
 $url = 'https://myshopifystore/admin/api/2019-07/products.json';
 $result = file_get_contents($url);
 $data = json_decode($result, true);
 dd($data);

my result like this picture:

【问题讨论】:

    标签: laravel shopify


    【解决方案1】:

    请求的限制属性默认为 50 到 250。如果您想要更多,请使用循环搜索每个页面直到结束。

    GET /admin/api/2019-07/products.json?limit=250?page=1
    

    使用

    GET /admin/api/2019-07/products/count.json 
    

    获取您要搜索的全部产品。

    关于分页页面here的更多信息

    编辑 1:分页 ?page= 现在已弃用。它将在 2020-07 版本中删除。您应该使用基于光标的分页。

    【讨论】:

    • 好的!谢谢!
    【解决方案2】:

    以下函数可以帮助在新的 shopify API 中使用基于光标的分页来获取资源

    public function request($method,$url,$param = []){
        $client = new \GuzzleHttp\Client();
        $url = 'https://'.$this->username.':'.$this->password.'@'.$this->domain.'/admin/api/2019-10/'.$url;
        $parameters = [
            'headers' => [
                'Content-Type' => 'application/json',
                'Accept' => 'application/json'
            ]
        ];
        if(!empty($param)){ $parameters['json'] = $param;}
        $response = $client->request($method, $url,$parameters);
        $responseHeaders = $response->getHeaders();
        $tokenType = 'next';
        if(array_key_exists('Link',$responseHeaders)){
            $link = $responseHeaders['Link'][0];
            $tokenType  = strpos($link,'rel="next') !== false ? "next" : "previous";
            $tobeReplace = ["<",">",'rel="next"',";",'rel="previous"'];
            $tobeReplaceWith = ["","","",""];
            parse_str(parse_url(str_replace($tobeReplace,$tobeReplaceWith,$link),PHP_URL_QUERY),$op);
            $pageToken = trim($op['page_info']);
        }
        $rateLimit = explode('/', $responseHeaders["X-Shopify-Shop-Api-Call-Limit"][0]);
        $usedLimitPercentage = (100*$rateLimit[0])/$rateLimit[1];
        if($usedLimitPercentage > 95){sleep(5);}
        $responseBody = json_decode($response->getBody(),true);
        $r['resource'] =  (is_array($responseBody) && count($responseBody) > 0) ? array_shift($responseBody) : $responseBody;
        $r[$tokenType]['page_token'] = isset($pageToken) ? $pageToken : null;
        return $r;
    }
    

    在控制器中使用上述函数

    $ids = [];
    $nextPageToken = null;
      do{
        $response = $shop->request('get','products.json?limit=250&page_info='.$nextPageToken.'&rel=next');
         foreach($response['resource'] as $product){
            array_push($ids, $product['id']);
         }
                $nextPageToken = $response['next']['page_token'] ?? null;
        }while($nextPageToken != null);
    

    【讨论】:

    • 感谢您的脚本,但是当我传递参数时,它显示 400 bad reuest 被截断。我想传递状态 => 'any',你有什么想法吗?
    • 尝试将其作为url参数传递
    • @BhargavKaklotara,以上可以在一个 Laravel 控制器中一起完成吗?因此,如果您点击路线“/getAllProducts”,它会触发控制器循环遍历所有产品并返回所有产品?我正在努力找出控制器中正在使用的功能?
    猜你喜欢
    • 1970-01-01
    • 2020-06-27
    • 2019-12-02
    • 1970-01-01
    • 2017-11-20
    • 2019-08-09
    • 2021-06-22
    • 1970-01-01
    • 2019-03-03
    相关资源
    最近更新 更多