phprestguzzleguzzle6

Get param 'Link' in the Header with Guzzle


I am using a third-party API. And the API has a pagination to navigate through the data. In the header I get the Link parameter where the next, previous, first and last are located, but when I get the Link param with Guzzle I get a *String*.

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

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

I got:

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

Is there a way to access the Link with the 'rel' keyword? Like it's an array? or in some other way?


Solution

  • You can use GuzzleHttp\Psr7\parse_header($response->getHeader('Link')) see the docs:

    $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')));