2014-02-23 34 views
4

我試圖從Goutte訪問Guzzle Response對象。因爲那個對象有我想要使用的很好的方法。例如getEffectiveUrl。訪問Guout來自Goutte的響應

就我所見,沒有黑客入侵代碼是沒有辦法的。

或者沒有訪問響應對象,有沒有辦法得到最後一個重定向的url foum goutte?

回答

11

晚了一點,但是:

如果你只讓你上次重定向到URL有興趣,你可以簡單地做

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

編輯:

但是,延長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; 
    } 
} 

然後根據需要

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

echo $response->getEffectiveUrl(); 
+0

是的,你可以訪問響應對象,我發現這一點。就我所知,這是獲取最後重定向網址的唯一方法。但我也想訪問Guzzle響應。 –

+0

我已經添加了一個簡單的方法來擴展您的需求的Goutte。希望能幫助到你。 – lazystar