How to parse Link data from header data in PHP?

Jivan_Suhagiya
Shopify Partner
573 79 126

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!

If helpful then please Like and Accept Solution.
Email: suhagiyajivan1992@gmail.com
Skype: jivan.suhagiya
First kind of Checkout Reminder APP: https://apps.shopify.com/checkout-reminder
Replies 5 (5)
Alex
Shopify Staff
Shopify Staff
1561 81 337

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

Trajche
Shopify Partner
1 0 0

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];
        }
    }

 

admin18
New Member
5 0 0

Got any reply regarding that issue.

Thanks

pk841442
New Member
1 0 0

what is $linkheader which is passed in function please tell.......

cottton
Shopify Partner
3 0 2

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',
)