Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Guzzle Response from Goutte

Tags:

php

guzzle

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?

like image 764
Can Vural Avatar asked Feb 23 '14 21:02

Can Vural


1 Answers

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();
like image 167
lazystar Avatar answered Nov 14 '22 08:11

lazystar