Search code examples
phpguzzlegoutte

Access Guzzle Response from Goutte


I'm trying to access to the Guzzle Response object from Goutte. Because that object has nice methods that i want to use. getEffectiveUrl for example.

As far as i can see there is no way doing it without hacking the code.

Or without accessing the response object, is there a way to get the last redirected url froum goutte?


Solution

  • A little late, but:

    If you are only interested in getting the URL you were last redirected to, you could simply do

    $client = new Goutte\Client();
    $crawler = $client->request('GET', 'http://www.example.com');
    $url = $client->getHistory()->current()->getUri();
    

    EDIT:

    But, extending Goutte to serve your needs is fairly easy. All you need is to override the createResponse() method and store the GuzzleResponse

    namespace Your\Name\Space;
    
    class Client extends \Goutte\Client
    {
        protected $guzzleResponse;
    
        protected function createResponse(\Guzzle\Http\Message\Response $response)
        {
            $this->guzzleResponse = $response;
    
            return parent::createResponse($response);
        }
    
        /**
         * @return \Guzzle\Http\Message\Response
         */
        public function getGuzzleResponse()
        {
            return $this->guzzleResponse;
        }
    }
    

    Then you can access the response object as desired

    $client = new Your\Name\Space\Client();
    $crawler = $client->request('GET', 'http://localhost/redirect');
    $response = $client->getGuzzleResponse();
    
    echo $response->getEffectiveUrl();