How to read the response effective URL in Guzzle ~6.0

后端 未结 6 1991
我寻月下人不归
我寻月下人不归 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:05

    You can check what redirects your request had byt setting track_redirects parameter:

    $client = new \GuzzleHttp\Client(['allow_redirects' => ['track_redirects' => true]]);
    
    $response = $client->request('GET', 'http://example.com');
    
    var_dump($response->getHeader(\GuzzleHttp\RedirectMiddleware::HISTORY_HEADER));
    

    If there were any redirects last one should be your effective url otherewise your initial url.

    You can read more about allow_redirects here http://docs.guzzlephp.org/en/latest/request-options.html#allow-redirects.

提交回复
热议问题