Rest API cursor based pagination returning empty header info for link

Highlighted
Shopify Partner
303 8 54

As many may have this question, I found that REST API version 2019-07 does not return the next or previous pagination URL.

 

All I get is:

X-Shopify-API-Version: 2019-07
Link: ; rel="next"

The url call is: /admin/api/2019-07/products.json?limit=2&published_status=published

 

I get 2 products, but the header does not give me the next url.

I'm a Google Shopping Specialist & a Google Ads Gold Product Expert - Specialized Data Feed Management Tool (DIY/Managed)
0 Likes
Shopify Partner
303 8 54

I found the issue, the link value is actually there, but you can't view it in the browser due to the angel brackets. If you view the source code, you can see the results.

I'm a Google Shopping Specialist & a Google Ads Gold Product Expert - Specialized Data Feed Management Tool (DIY/Managed)
2 Likes
Highlighted
Shopify Partner
9 0 0

I have the same issue. Can you please explain me How would you get page_info parameter from Link? I can see it in source code. But it is in <https></https tag.

 

How to process it? I am using PHP. Please help me.

0 Likes
Highlighted
Shopify Partner
12 0 6

@EmmanuelFlossie thanks for pointing this out, it saved me a headache!

 

@Dhara_Prajapati after you get the headers with curl, here's an idea how you can separate the pagination links

$prev = false;
$next = false;

if($headers['link']) {
	$links = explode(',', $headers['link']);
	foreach($links as $link) {
		if(strpos($link, 'rel="previous"')) {
			preg_match('~<(.*?)>~', $link, $prev);
		} elseif(strpos($link, 'rel="next"')) {
			preg_match('~<(.*?)>~', $link, $next);
		}
	}
}
echo $prev[1];
echo '<hr>';
echo $next[1];

Output

https://{your-shop}.myshopify.com/admin/api/2020-01/orders.json?limit=5&page_info={your-prev-page-key}
<hr>
https://{your-shop}.myshopify.com/admin/api/2020-01/orders.json?limit=5&page_info={your-next-page-key}

 

1 Like
Highlighted
New Member
5 0 0

how can I get 'your-prev-page-key' 

I used below sample code -

 

$url = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/admin/api/2020-01/orders.json?processed_at_min='.$GetOrderDate.'&processed_at_max='.$GetOrderDate.'&status=any&financial_status=paid,partially_refunded,refunded&fields=created_at,id,name&limit=250&;rel="next"';
        $session = curl_init(); 
        curl_setopt($session, CURLOPT_URL, $url);
curl_setopt($session, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($session, CURLOPT_HEADER, false);
curl_setopt($session, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json'));
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
if(preg_match("^(https)^",$url)) curl_setopt($session,CURLOPT_SSL_VERIFYPEER,false);
$result = curl_exec($session);
$orders = json_decode($result)->orders;
 
echo '<pre>';
print_r($orders);
echo '</pre>';
0 Likes