2 回答
TA贡献1966条经验 获得超4个赞
为所有类型的 URL 返回 page_info。
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;
}
响应转储
array(2) {
["previous"]=>
string(143) "eyJwdWJsaXNoZWRfc3RhdHVzIjoicHVibGlzaGVkIiwiZGlyZWN0aW9uIjoicHJldiIsImxhc3RfaWQiOjM3NDc0NDA1NTgxNjQsImxhc3RfdmFsdWUiOiJhbHBoYWJldCB0LXNoaXJ0In0"
["next"]=>
string(188) "eyJwdWJsaXNoZWRfc3RhdHVzIjoicHVibGlzaGVkIiwiZGlyZWN0aW9uIjoibmV4dCIsImxhc3RfaWQiOjE1MjAyMzAzMzQ1NDgsImxhc3RfdmFsdWUiOiJBc3ltbWV0cmljIEhpZ2ggV2Fpc3QgU2hvcnQgU2xlZXZlIFJlZCBQYXJ0eSBEcmVzcyJ9"
}
TA贡献1841条经验 获得超3个赞
你可以试试我写的这个功能,只需将链接文本或整个标题信息发送给它
private function render_links($Link)
{
$combined_links = [];
$links = explode(',', $Link);
foreach ($links as $link){
if (preg_match('#<(http(?:s)?:\/\/.*\.myshopify.com\/.*products.json\?.*)>;.*rel=\\"(.*)\\"#', $link, $matches)) {
$combined_links[$matches[2]] = $matches[1];
}
}
return $combined_links;
}
它将像这样的链接作为数组返回
"previous":"limit=1&fields=id%2Cimages&page_info=eyJkaXJlY3Rpb24iOiJwcmV2IiwibGFzdF9pZCI6NDA5OTUyMjM5NjE5NSwibGFzdF92YWx1ZSI6InNmc2Rmc2YifQ",
"next":"limit=1&fields=id%2Cimages&page_info=eyJkaXJlY3Rpb24iOiJuZXh0IiwibGFzdF9pZCI6NDA5OTUyMjM5NjE5NSwibGFzdF92YWx1ZSI6InNmc2Rmc2YifQ"
- 2 回答
- 0 关注
- 228 浏览
添加回答
举报