Development discussions around Shopify APIs
Hi,
Can anyone guide me how to parse next and previous page link from below code using PHP?
#... 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}" #...
Thank you!
Try something like preg_match()
:
preg_match("/<(.*)>; rel=\"next\"/", $link_str, $matches);
$matches[1]
in this case will end up being the matching substring representing the next page link. I'm not a PHP developer so there may be another way, and there's probably a better regular expression to use, but I feel that's a good stating point.
Cheers.
Alex | Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit the Shopify Help Center or the Shopify Blog
Using the code below, you can call returnHeaderArray() on your 'Link' header and it will return an array that will contain previous and next links if available.
function returnHeaderArray($linkHeader) { $cleanArray = []; if (strpos($linkHeader, ',') !== false) { //Split into two or more elements by comma $linkHeaderArr = explode(',', $linkHeader); } else { //Create array with one element $linkHeaderArr[] = $linkHeader; } foreach ($linkHeaderArr as $linkHeader) { $cleanArray += [ extractRel($linkHeader) => extractLink($linkHeader) ]; } return $cleanArray; } function extractLink($element) { if (preg_match('/<(.*?)>/', $element, $match) == 1) { return $match[1]; } } function extractRel($element) { if (preg_match('/rel="(.*?)"/', $element, $match) == 1) { return $match[1]; } }
Got any reply regarding that issue.
Thanks
what is $linkheader which is passed in function please tell.......
Old but if somebody needs it:
// Use your http client|response to get the response header value from the offset "Link":
$headerValue = $response->getHeader('Link')
// File "previous" and "next" links:
$links = [];
foreach (explode(',', $headerValue) as $string) {
if (preg_match("/<(?<link>[^>]+)>;\s*rel=\"?(?<rel>[a-z]+)\"?/i", $string, $matches) === 1) {
if (!isset($matches['rel'])) {
throw new \Exception("Cannot parse header value. Missing matches[rel].");
}
if (!isset($matches['link'])) {
throw new \Exception("Cannot parse header value. Missing matches[link].");
}
$links[trim($matches['rel'])] = trim($matches['link']);
}
}
Return example using documentation example header string:
array (
'previous' => 'https://{shop}.myshopify.com/admin/api/2019-07/products.json?page_info=abcdefg&limit=3',
'next' => 'https://{shop}.myshopify.com/admin/api/2019-07/products.json?page_info=opqrstu&limit=3',
)
Return example using real world example header string (with no "previous" link):
array (
'next' => 'https://myshop-dev.myshopify.com/admin/api/2022-10/products.json?limit=3&page_info=eyJsYXN0X2lkIjo4MDI3MTQ5ODYxMTcwLCJsYXN0X3ZhbHVlIjoiSVBvZCBOYW5vIC0gOEdCIiwiZGlyZWN0aW9uIjoibmV4dCJ9',
)
Thanks to all Community members that participated in our inaugural 2 week AMA on the new E...
By Jacqui Mar 10, 2023Upskill and stand out with the new Shopify Foundations Certification program
By SarahF_Shopify Mar 6, 2023One of the key components to running a successful online business is having clear and co...
By Ollie Mar 6, 2023