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
Share on other sites

Hi,

 

thanks for reading. Yes it would, but the code I have is only outputting URL2, i.e. the first redirect. I can't get it to output all of them. Do I need a loop or does the PHP version of cURL have it as a built in function that it loops through?

Link to comment
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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.