Jump to content

cURL - Following Multiple Redirects


evasec

Recommended Posts

Hi All,

 

I'm pretty new to PHP so excuse my n00b questions but...

 

I want to output all redirection headers that result from a single URL using cURL. For example, if I have;

 

URL1 -> URL2 -> URL3

 

where -> means redirect

 

I want to get URL2 and URL3 from entering URL1. In cURL it would be like this

 

curl -I -L http://raoul.shacknet.nu/servers/vnc.html

 

..but I don't know how to output it properly in PHP. Does anyone know How to solve this problem? At the moment I have the following PHP code which gets me to URL2 but not URL3:

 

<?php

if($_POST['Submit'])
{
$redirect = $_POST['redirect'];

$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_URL,$redirect);
curl_setopt($curl_handle,CURLOPT_FOLLOWLOCATION);
curl_setopt($curl_handle,CURLOPT_NOBODY);
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,true);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);

print $buffer;
}
else
{
print <<<ENDOFTXT
</style><form name="form1" method="post" action="curltest.php">
<table width="300" border="0" align="left">
<tr>
<td width="150"><span class="style5">URL:</span></td>
<td width="150"><input name="redirect" type="text" id="redirect"></td>
<input name="Submit" type="submit" class="style5" value="Submit"></td>
</tr>
<tr>
<td colspan="2"><div align="center"></div></td>
</tr>
</table>
<p align="center"> </p>
</form>
ENDOFTXT;
}

?>

 

Thanks in advance to anyone that can help me out here..

 

Evasec

Link to comment
https://forums.phpfreaks.com/topic/157103-curl-following-multiple-redirects/
Share on other sites

Hi All,

Apologies if I didn't explain this in enough detail. The output from 'curl -I -L http://raoul.shacknet.nu/servers/vnc.html' is like this. I'm looking to get the same using PHP and cURL. You can see that there are several redirects.

 

HTTP/1.1 301 Moved Permanently
Vary: Accept-Encoding
Location: http://www.raoul.shacknet.nu/servers/vnc.html
Content-Type: text/html; charset=iso-8859-1
Date: Wed, 06 May 2009 16:16:18 GMT
Server: Apache/2.2.11

HTTP/1.1 301 Moved Permanently
Vary: Accept-Encoding
Location: http://www.g-loaded.eu/servers/vnc.html
Content-Type: text/html; charset=iso-8859-1
Date: Wed, 06 May 2009 16:16:18 GMT
Server: Apache/2.2.11

HTTP/1.1 301 Moved Permanently
Vary: Accept-Encoding
Location: http://www.g-loaded.eu/2005/11/10/configure-vnc-server-in-fedora/
Content-Type: text/html; charset=iso-8859-1
Date: Wed, 06 May 2009 16:16:18 GMT
Server: Apache/2.2.11

HTTP/1.1 200 OK
Vary: Accept-Encoding
ETag: "29d6f1-f3c8-4693d76afd900"
Accept-Ranges: bytes
Expires: Wed, 06 May 2009 16:21:18 GMT
Content-Length: 62408
Last-Modified: Wed, 06 May 2009 12:23:32 GMT
Content-Type: text/html; charset=UTF-8
Cache-Control: max-age=300, must-revalidate
Date: Wed, 06 May 2009 16:16:18 GMT
Server: Apache/2.2.11

Thanks Ken2k7,

 

I don't have control over the pages, I'm trying to build a tool that will output the last (and real) location of the file. In this example it's http://www.g-loaded.eu/2005/11/10/configure-vnc-server-in-fedora/. So I guess I'm looking for something that says:

 

http://raoul.shacknet.nu/servers/vnc.html redirects to http://www.g-loaded.eu/2005/11/10/configure-vnc-server-in-fedora/

 

I can then input the urls and find out where they actually end up.

 

Cheers for helping here!

Sound like that :

 

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10); /* Max redirection to follow */

 

CURLOPT_FOLLOWLOCATION

TRUE to follow any "Location: " header that the server sends as part of the HTTP header (note this is recursive, PHP will follow as many "Location: " headers that it is sent, unless CURLOPT_MAXREDIRS is set).

http://www.php.net/manual/en/function.curl-setopt.php

 

I'm not sure exactly if

curl_setopt($curl_handle,CURLOPT_FOLLOWLOCATION);

set it to true...or not

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.