'How to find page_info in Shopify PHP app and how to print all products on it?
I tried this for the app but it is not working:
$products = shopify_call($token, $shop, "/admin/api/2022-04/products/count.json", 'GET');
$response = json_decode($products['response'], JSON_PRETTY_PRINT);
$totalproducts = $response['count'];
$limit = 250;
$totalpage = ceil($totalproducts/$limit);
echo $totalpage;
$k = 0;
Here i don't have the page_info. How can I find it? On some research I found that there is on response header have link parameter where rel have next or previous but I have link parameter but it like something this:
[link] => max-age=1234567
for($i=1; $i<=$totalpage; $i++) {
$products = shopify_call($token, $shop, '/admin/api/2022-04/products.json?page_info='.$i.'&limit=250', 'GET');
$response = json_decode($products['response'], JSON_PRETTY_PRINT);
// print_r($response);
foreach($response['products'] as $prod) {
$k++;
echo $k . ' => ' . $prod['title']. ' => ' . $prod['id'];
echo "<br>";
}
}
How to find page_info and how to use it? I'm using core PHP.
Solution 1:[1]
I don't know what exactly does you shopify_call(), but yes you're on the right track about the headers, there should be the info for your next page. Here is a var_dump of my "next page" header when loading products:
[18] =>
array(2) {
'name' =>
string(4) "Link"
'line' =>
string(191) "Link: <https://my-test-page.myshopify.com/admin/api/2021-10/products.json?limit=5&page_info=eyJsYXN0X2lkIjo0MzY3MzE3MzAzMzM5LCJsYXN0X3ZhbHVlIjoiVGVzdCAyIiwiZGlyZWN0aW9uIjoibmV4dCJ9>; rel="next""
}
In the response there are a bunch of headers but the one with the info for the next page is named "Link" as you can see from the dump. As I said IDK what you have inside your shopify_call method, it is up to you to find the right way to get response headers and if "Link" header to extract the line, which is this string:
"<https://my-test-page.myshopify.com/admin/api/2021-10/products.json?limit=5&page_info=eyJsYXN0X2lkIjo0MzY3MzE3MzAzMzM5LCJsYXN0X3ZhbHVlIjoiVGVzdCAyIiwiZGlyZWN0aW9uIjoibmV4dCJ9>; rel="next""
To extract just the link for your next request you can use preg_match() with this regex:
// let's assume you've extracted and stored the string in your response
// as $products['nextLink']
$matches = [];
if (! empty($products['nextLink'])) {
preg_match(
"/<(.*)>; rel=\"next\"/",
$products['nextLink'],
$matches
);
// extracted url will be stored at 1-st key in $matches array
$nextLink = $matches[1];
}
var_dump($matches);
array(2) {
[0]=>
string(187) "<https://my-test-page.myshopify.com/admin/api/2021-10/products.json?limit=5&page_info=eyJsYXN0X2lkIjo0MzY3MzE3MzAzMzM5LCJsYXN0X3ZhbHVlIjoiVGVzdCAyIiwiZGlyZWN0aW9uIjoibmV4dCJ9>; rel="next""
[1]=>
string(173) "https://my-test-page.myshopify.com/admin/api/2021-10/products.json?limit=5&page_info=eyJsYXN0X2lkIjo0MzY3MzE3MzAzMzM5LCJsYXN0X3ZhbHVlIjoiVGVzdCAyIiwiZGlyZWN0aW9uIjoibmV4dCJ9"
}
If there is no next page, this header won't be presented in the response headers, that's it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Angel Deykov |
