2

Making a simple request like:

$client = new Zend_Http_Client('http://example.org');
$response = $client->request();

How can I get the final URL after the redirects? I have not seen a way in the documentation or the API docs unless I'm missing something.

Thanks in advance.

clips404
  • 33
  • 4

3 Answers3

2

Zend_Http_Client update the last URL into Zend_Http_Client->uri property if there is redirect.

$sourceUrl = 'http://google.com';
$client = new Zend_Http_Client($sourceUrl); 
$response = $client->request(); 
$finalUrl = $client->getUri()->__toString();

var_dump($sourceUrl);
// string(17) "http://google.com"
var_dump($finalUrl);
// string(25) "http://www.google.com:80/" 
hthetiot
  • 359
  • 4
  • 8
0

Not tested :

$response->getHeader('Location');
Maxence
  • 12,868
  • 5
  • 57
  • 69
  • Location is contained in the header when there is a redirect. The header does not contain the final URL. I have given up and started to use curl. Thanks for trying though! – clips404 Jan 11 '11 at 16:30
0

Get the last request from the client and then extract the headers.

$client = new Zend_Http_Client('http://webonyx.com');
$response = $client->request();
$lastHeaders = Zend_Http_Response::extractHeaders($client->getLastRequest());

// $lastHeaders['host'] will be your final redirected host