来自Goutte的Guzzle回应

Can*_*ral 5 php goutte guzzle

我正在尝试从Goutte访问Guzzle Response对象.因为该对象具有我想要使用的好方法.以getEffectiveUrl为例.

据我所知,如果不破解代码,就无法做到这一点.

或者没有访问响应对象,有没有办法获取最后重定向的URL froum goutte?

laz*_*tar 12

有点晚了,但是:

如果您只想获取上次重定向到的URL,您可以这样做

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

编辑:

但是,扩展Goutte以满足您的需求是相当容易的.您所需要的只是覆盖createResponse()方法并存储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;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以根据需要访问响应对象

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

echo $response->getEffectiveUrl();
Run Code Online (Sandbox Code Playgroud)