使用 Guzzle 获取标题中的参数“Link”

问题描述 投票:0回答:1

我正在使用第三方 API。 API 具有分页功能来浏览数据。在标题中,我得到了下一个、上一个、第一个和最后一个所在的链接参数。但是当我通过

Link
获得
Guzzle
参数时,我得到一个
*String*

....
$responseAPI = $httpClient->get($uri);

$linkHeader = $responseAPI->getHeader("Link");
....

我得到:

'http://next.com"; rel="next", http://next.com"; rel="prev"'

有没有办法使用“rel”关键字访问

Link
?好像是一个数组?或者以其他方式?

rest api guzzle guzzle6
1个回答
3
投票

您可以使用

GuzzleHttp\Psr7\parse_header($responce->getHeader('Link'))
查看文档

$client = new \GuzzleHttp\Client();
    
$response = $client->get('https://httpbin.org/response-headers?Link=http://next.com?3;rel="next",http://next.com?1;rel="prev"');

print_r(GuzzleHttp\Psr7\parse_header($response->getHeader('Link')));
© www.soinside.com 2019 - 2024. All rights reserved.