Paginate the 2019-07 Products Api with PHP

Highlighted
Shopify Partner
6 0 0

Hello,

 

Any body help me how can i get the products of 1,2,3 pages etc. New api not supporting the page attribute, so which variable i can use to paginate the products.

 

Here Code:

$response = $client->request(
'GET', 
[
'query' => [
'page_info' => '',
'fields' => 'id,title',
 'limit' => 250,
'access_token' => $access_token
]
]
); 
 
With this code, i am only getting first page products in response. How i can get next products?
 
 
Thanks
0 Likes
Highlighted

Hello 

Please refer below link:

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

Want to modify or incorporate custom changes on store, Hire us.
If helpful then please Like and Accept Solution .
Email: pallavi@oscprofessionals.com
Skype : oscprofessionals-87
Free Review and advice for sale on store | Shopify Private App | Custom Theme Development | Custom Modifications In To Shopify Theme | SEO & Digital Marketing | Site Speed Optimization | Performance Site Audit
0 Likes
Highlighted
Shopify Partner
11 0 3

@harsimer_shine, searching around there web, I couldn't find resources on the new way to paginate the 2019/2020 REST API calls with PHP either. After a night of messing around with it I managed to come up with this code, that should give you (and anyone else for that matter) an idea of how it works.

 

$items_per_page = 50;
$merged = array(); $next_page = ''; $last_page = false; while(!$last_page) { $url = 'https://{username}:{password}@{shop}.myshopify.com/admin/api/2020-01/smart_collections.json?limit=' . $items_per_page . $next_page; // . '&fields='; $curl = curl_init(); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HEADERFUNCTION, function($curl, $header) use (&$headers) { $len = strlen($header); $header = explode(':', $header, 2); if (count($header) >= 2) { $headers[strtolower(trim($header[0]))] = trim($header[1]); } return $len; }); $result = curl_exec($curl); curl_close($curl); if(isset($headers['link'])) { $links = explode(',', $headers['link']); foreach($links as $link) { if(strpos($link, 'rel="next"')) { preg_match('~<(.*?)>~', $link, $next); $url_components = parse_url($next[1]); parse_str($url_components['query'], $params); $next_page = '&page_info=' . $params['page_info']; } else { $last_page = true; } } } else {
$last_page = true; // if missing "link" parameter - there's only one page of results = last_page
} $source_array = json_decode($result, true); $merged = array_merge_recursive($merged, $source_array); } print_r($merged);
0 Likes