如何根据新的分页更新解析shopify分页链接的字符串?

我有一个包含上一页链接和下一页链接的字符串:


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}"

期待输出


array(

    'next' => {next_page_info},

    'prev' => {prev_page_info}

);

链接字符串也有一些条件如下:

  1. 字符串同时具有下一个和上一个链接

  2. 字符串只有下一个链接 输出应该是 array('next' => '{next_page_info}' );

  3. 字符串只有 prev 链接输出应该是 array('prev' => '{prev_page_info}');


墨色风雨
浏览 229回答 2
2回答

慕标5832272

为所有类型的 URL 返回 page_info。public function parsePaginationLinkHeader ($headerLink)&nbsp;{&nbsp; &nbsp; $available_links = [];&nbsp; &nbsp; $links = explode(',', $headerLink);&nbsp; &nbsp; foreach ($links as $link){&nbsp; &nbsp; &nbsp; &nbsp; if (preg_match('/<(.*)>;\srel=\\"(.*)\\"/', $link, $matches)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $query_str = parse_url($matches[1], PHP_URL_QUERY);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parse_str($query_str, $query_params);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $available_links[$matches[2]] = $query_params['page_info'];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return $available_links;}响应转储array(2) {["previous"]=>string(143) "eyJwdWJsaXNoZWRfc3RhdHVzIjoicHVibGlzaGVkIiwiZGlyZWN0aW9uIjoicHJldiIsImxhc3RfaWQiOjM3NDc0NDA1NTgxNjQsImxhc3RfdmFsdWUiOiJhbHBoYWJldCB0LXNoaXJ0In0"["next"]=>string(188) "eyJwdWJsaXNoZWRfc3RhdHVzIjoicHVibGlzaGVkIiwiZGlyZWN0aW9uIjoibmV4dCIsImxhc3RfaWQiOjE1MjAyMzAzMzQ1NDgsImxhc3RfdmFsdWUiOiJBc3ltbWV0cmljIEhpZ2ggV2Fpc3QgU2hvcnQgU2xlZXZlIFJlZCBQYXJ0eSBEcmVzcyJ9"}

偶然的你

你可以试试我写的这个功能,只需将链接文本或整个标题信息发送给它&nbsp;private function render_links($Link)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $combined_links = [];&nbsp; &nbsp; &nbsp; &nbsp; $links = explode(',', $Link);&nbsp; &nbsp; &nbsp; &nbsp; foreach ($links as $link){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (preg_match('#<(http(?:s)?:\/\/.*\.myshopify.com\/.*products.json\?.*)>;.*rel=\\"(.*)\\"#', $link, $matches)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $combined_links[$matches[2]] = $matches[1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return $combined_links;&nbsp; &nbsp; }它将像这样的链接作为数组返回"previous":"limit=1&fields=id%2Cimages&page_info=eyJkaXJlY3Rpb24iOiJwcmV2IiwibGFzdF9pZCI6NDA5OTUyMjM5NjE5NSwibGFzdF92YWx1ZSI6InNmc2Rmc2YifQ","next":"limit=1&fields=id%2Cimages&page_info=eyJkaXJlY3Rpb24iOiJuZXh0IiwibGFzdF9pZCI6NDA5OTUyMjM5NjE5NSwibGFzdF92YWx1ZSI6InNmc2Rmc2YifQ"
打开App,查看更多内容
随时随地看视频慕课网APP