Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parsing string of shopify pagination link according new pagination update?

I Have one string which contains the link of prev and next page link:

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

expecting output

array(
    'next' => {next_page_info},
    'prev' => {prev_page_info}
);

link string has also some condition as below:

  1. string has both next and prev link

  2. string has only next link Output should be array('next' => '{next_page_info}' );

  3. string has only prev link Output should be array('prev' => '{prev_page_info}');

like image 596
Sachin Sarola Avatar asked Jan 01 '23 16:01

Sachin Sarola


1 Answers

To return page_info for all types of URL.

public function parsePaginationLinkHeader ($headerLink) 
{

    $available_links = [];
    $links = explode(',', $headerLink);
    foreach ($links as $link){

        if (preg_match('/<(.*)>;\srel=\\"(.*)\\"/', $link, $matches)) {

            $query_str = parse_url($matches[1], PHP_URL_QUERY);
            parse_str($query_str, $query_params);
            $available_links[$matches[2]] = $query_params['page_info'];
        }
    }

    return $available_links;
}

Response Dump

array(2) {
["previous"]=>
string(143) "eyJwdWJsaXNoZWRfc3RhdHVzIjoicHVibGlzaGVkIiwiZGlyZWN0aW9uIjoicHJldiIsImxhc3RfaWQiOjM3NDc0NDA1NTgxNjQsImxhc3RfdmFsdWUiOiJhbHBoYWJldCB0LXNoaXJ0In0"
["next"]=>
string(188) "eyJwdWJsaXNoZWRfc3RhdHVzIjoicHVibGlzaGVkIiwiZGlyZWN0aW9uIjoibmV4dCIsImxhc3RfaWQiOjE1MjAyMzAzMzQ1NDgsImxhc3RfdmFsdWUiOiJBc3ltbWV0cmljIEhpZ2ggV2Fpc3QgU2hvcnQgU2xlZXZlIFJlZCBQYXJ0eSBEcmVzcyJ9"
}
like image 183
xyz Avatar answered Jan 08 '23 02:01

xyz