How to read the response effective URL in Guzzle ~6.0

后端 未结 6 1989
我寻月下人不归
我寻月下人不归 2020-12-24 01:26

I\'ve been searching for about 2 hours and I can\'t figure it out how to read the final response uri.

In previous versions of PHP Guzzle you just call

6条回答
  •  误落风尘
    2020-12-24 02:15

    I think it's best to use response headers to store this information. I wrote a simple class that saves effective url in X-GUZZLE-EFFECTIVE-URL header:

    https://gist.github.com/Thinkscape/43499cfafda1af8f606d

    Usage:

    push(EffectiveUrlMiddleware::middleware());
    $client = new Client(['handler' => $stack]);
    
    // Test it out!
    $response = $client->get('http://bit.ly/1N2DZdP');
    echo $response->getHeaderLine('X-GUZZLE-EFFECTIVE-URL');
    

提交回复
热议问题