Tuesday 24 September 2019

How to create pagination in shopify rest api using php

Shopify new API next page issue | page_info => invalid value

Shopify API version 2019-07 you do indeed need to use cursor-based pagination.

First make a normal API call, include the header response.

Then in the header response API, you will see the "link" parameter with rel next or previous.

Extract the page_info and then make another call with page_info.
The first API call is something like this:

https://.....@abc.myshopify.com/admin/api/2019-10/products.json?limit=2&published_status=published
To get header response from API response :
function getheaderarray($apiresponse){
       
        $data = explode("\n",$apiresponse);
        $headers['status'] = $data[0];
        array_shift($data);
        foreach($data as $part){
            $middle = explode(":",$part,2);
            if ( !isset($middle[1]) ) { $middle[1] = null; }
            $headers[trim($middle[0])] = trim($middle[1]);
        }
       
        return $headers;
  }  
Then the second API call is something like this:
https://.....@abc.myshopify.com/admin/api/2019-10/products.json?limit=2&page_info=abcdsf21asdf3afdfdfd1safd51s3f1x32ref4asdj
When you make the second call, do not add any filers as the filters will be applied from the first call.
FYI: if your testing in a browser due to the way the link is in angle brackets, it will be hidden, so just view source code in your developer environment.

3 comments:

  1. Hi
    I am using this way to get all orders from shopify backend.
    ===========
    $orders = $shopify('GET /admin/api/2020-07/orders.json?query=&limit=250&status=any', array('published_status'=>'published'));
    ===========
    Please let me know how I can use pagination in this way.
    Thanks

    ReplyDelete
    Replies
    1. You can use this API for first call after that you get the "next page encrypt data", you need to pass that data on next request. "next page encrypt data" you can get CURL result header information. let me know still you are facing any issue on that.

      Delete
  2. This is my view of code:
    https://prnt.sc/zklgzc

    ReplyDelete