evasec Posted May 6, 2009 Share Posted May 6, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/157103-curl-following-multiple-redirects/ Share on other sites More sharing options...
Ken2k7 Posted May 6, 2009 Share Posted May 6, 2009 Wouldn't URL1 -> URL2 -> URL3 the same as URL1 -> URL3? Quote Link to comment https://forums.phpfreaks.com/topic/157103-curl-following-multiple-redirects/#findComment-827655 Share on other sites More sharing options...
evasec Posted May 6, 2009 Author Share Posted May 6, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/157103-curl-following-multiple-redirects/#findComment-827664 Share on other sites More sharing options...
Ken2k7 Posted May 6, 2009 Share Posted May 6, 2009 cURL will redirect to URL2, but then it's up to URL2's job to redirect to URL3. So maybe add something to URL2's page? Quote Link to comment https://forums.phpfreaks.com/topic/157103-curl-following-multiple-redirects/#findComment-827670 Share on other sites More sharing options...
evasec Posted May 6, 2009 Author Share Posted May 6, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/157103-curl-following-multiple-redirects/#findComment-827681 Share on other sites More sharing options...
evasec Posted May 6, 2009 Author Share Posted May 6, 2009 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! Quote Link to comment https://forums.phpfreaks.com/topic/157103-curl-following-multiple-redirects/#findComment-827695 Share on other sites More sharing options...
gffg4574fghsDSGDGKJYM Posted May 6, 2009 Share Posted May 6, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/157103-curl-following-multiple-redirects/#findComment-827719 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.