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
Whats app
Free Website Review and advice for sale from Shopify Conversion Experts | Shopify Private App | Custom Theme Development | Custom Modifications In To Shopify Theme | Shopify SEO ; Digital Marketing & amp | Site Speed Optimization | Performance Site Audit
0 Likes
Highlighted
Shopify Partner
12 0 6

@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);
1 Like
Highlighted
Shopify Partner
7 0 4

Hey @Georgi_Demirev ,

 

this is awesome thanks for sharing this!

 

When I run this it only prints the first two pages of results though? 

case:

Set $items_per_page = 10; 

Result: 20 products

 

Is there a specific reason in the code as to why that is happening?

The store I am querying has close on 2K products.

 

Any help would be greatly appreciated.

 

Regards

 

 

0 Likes
Highlighted
Shopify Partner
12 0 6

@Simon_Miszewski there was a bug in the loop somewhere, but I can't quite recall what was causing the issue.

 

Here's my current function that works as intended, use it as you may:

 

function shopify_curl_get($request, $fields = '', $endpoints = '', $limit = 50, $no_pagination = false) {
	$merged = array();
	$page_info = '';
	$last_page = false;
	$limit = '?limit=' . $limit;
	$debug = 0;

	while(!$last_page) {
		$url = $request . $limit . $fields . $endpoints . $page_info;
		$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) {
				$next_page = false;
				if(strpos($link, 'rel="next"')) {
					$next_page = $link;
				}
			}

			if($next_page) {
				preg_match('~<(.*?)>~', $next_page, $next);
				$url_components = parse_url($next[1]);
				parse_str($url_components['query'], $params);
				$page_info = '&page_info=' . $params['page_info'];
				$endpoints = ''; // Link pagination does not support endpoints on pages 2 and up
			} else {
				$last_page = true; // There's no next page, we're at the last page
			}

		} else {
			$last_page = true; // Couldn't find parameter link in headers, stop loop
		}
		$source_array = json_decode($result, true);
		$merged = array_merge_recursive($merged, $source_array);

		if($no_pagination) {
			$last_page = true;
		}

		// Used for debugging to prevent infinite loops, comment to disable
		//if($debug >= 150) {
		//	//break;
		//}
		//$debug++;

		sleep(1); // Limit calls to 1 per second
	}

	return $merged;
}


Authenticating

$username = 'USERNAME';
$password = 'PASSWORD';
$shop = 'SHOP';
$api = 'api/2020-01/';
$auth = 'https://' . $username . ':' . $password . '@' . $shop . '.myshopify.com/admin/' . $api;

Example call

$request = $auth . 'products.json';
$fields = '&fields=id,created_at,handle,tags,variants,images';
$endpoints = '&published_status=published';
$products = shopify_curl_get($request, $fields, $endpoints, 250);

print_r($products);
2 Likes
Highlighted
Shopify Partner
7 0 4

@Georgi_Demirev , Legend thank you so much!

 

Your solution worked perfectly and was able to return all products.

0 Likes