How to parse Link data from header data in PHP?

Shopify Partner
573 79 126

Can anyone guide me how to parse next and previous page link from below code using PHP?

Link: "<https://{shop}{version}/products.json?page_info={page_info}&limit={limit}>; rel={next}, 
<https://{shop}{version}/products.json?page_info={page_info}&limit={limit}>; rel={previous}"

Thank you!

If helpful then please Like and Accept Solution.
Skype: jivan.suhagiya
First kind of Checkout Reminder APP:
Replies 5 (5)
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.



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

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


New Member
5 0 0

Got any reply regarding that issue.


New Member
1 0 0

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

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:
= [];
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}',
'next' => 'https://{shop}',

 Return example using real world example header string (with no "previous" link):

array (
'next' => '',