skip to Main Content

I have so many pages of products on my development store.But when I try to use products.json to get all of my products, it only take 50 listings(one page).Could anybody solve my doubt?

<?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:

2

Answers


  1. The limit property on the request has a default of 50 up to 250. In case you want more use a loop to search though every page until the end.

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

    Use

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

    to get the total product you want to search.

    More info about paginated page here

    EDIT 1 : The paginated ?page= is now deprecated. It will be remove in version 2020-07. You should use cursor-based pagination.

    Login or Signup to reply.
  2. Below function can help to fetch resources with cursor based pagination in new shopify API

    public function request($method,$url,$param = []){
        $client = new GuzzleHttpClient();
        $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;
    }
    

    using above function in controller

    $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);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search