How to create pagination in shopify rest api using php

。_饼干妹妹 提交于 2020-03-10 05:12:46

问题


I created curl in php for use shopify product rest API. Now I want to create pagination in that.

How to create?

Link: "<https://{shop}.myshopify.com/admin/api/{version}/products.json?page_info={page_info}&limit={limit}>; rel={next}, <https://{shop}.myshopify.com/admin/api/{version}/products.json?page_info={page_info}&limit={limit}>; rel={previous}"

How to use Link ?

Thanks


回答1:


As of 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 you will see link : ... with rel next or previous.

Extract the page_info and then make another call with page_info.

The first call is something like this:

https://...:...@xyz.myshopify.com/admin/api/2019-10/products.json?limit=2&published_status=published

Then the second call is

https://...:...@xyz.myshopify.com/admin/api/2019-10/products.json?limit=2&page_info=asdfas1321asdf3as1f651saf61s3f1x32v1akjhfasdj

When you make the second call, remove any filers as the filters will be applied from the first call.

Btw: 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.

Reference: https://help.shopify.com/en/api/guides/paginated-rest-results




回答2:


Below function can help you. to fetch data using API in Php/Laravel

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;
}

Example usage of this function

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

If you want to use graphQL api in php / laravel then below post can help you

How to request shopify graphql-admin-api from an api?




回答3:


The Rest API has no next feature. You are making that up. If you wanted to use a next pagination style of call, you'd be using the GraphQL API endpoint instead, and you would be using the cursor feature there, which has a next and previous concept built right in.

So either you replace your next wish with an actual page number which does work with the Rest API, or you switch to GraphQL and offer up next and previous using cursors.



来源:https://stackoverflow.com/questions/57801924/how-to-create-pagination-in-shopify-rest-api-using-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!