Advertisement
aeroson

PHP: lame recursive curl

Jan 31st, 2013
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.62 KB | None | 0 0
  1. <?
  2.  
  3.  
  4. function getContents($url,$count=0)
  5. {
  6.  
  7. echo "<h2>getContents at time: ".time()."</h2>";
  8.  
  9. $cookie="cookie.txt";
  10.  
  11. $ch=curl_init();
  12. curl_setopt( $ch, CURLOPT_URL, $url );
  13. curl_setopt($ch, CURLOPT_HEADER, TRUE);
  14. if(file_exists($cookie)) curl_setopt($ch, CURLOPT_COOKIE, file_get_contents($cookie));
  15. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
  16. curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  17.  
  18. $content = curl_exec( $ch );
  19. $info  = curl_getinfo( $ch );
  20.  
  21. curl_close ( $ch );  
  22.  
  23. echo"<h2>url</h2>".$url;
  24.  
  25. echo"<br><h2>$cookie to send</h2>";
  26. readfile($cookie);
  27.  
  28. echo"<br><h2>response</h2>";
  29.  
  30. $content=split("\r\n",$content); // parse response headers
  31. echo"<table>";
  32. $redirect="";
  33. foreach($content as $c) // go thru all headers
  34. {
  35.   if($k!="Content-Type") // after this header is usually site html
  36.   {
  37.     list($k,$v)=split(": ",$c); // parse heading data
  38.     echo"<tr><td>$k</td><td>$v</td></tr>"; // show it
  39.    
  40.     if($k=="Set-Cookie") // if cookie, save it
  41.     {
  42.       $fh = fopen($cookie, 'w');
  43.       fwrite($fh, $v);
  44.       fclose($fh);
  45.     }
  46.     if($k=="Location") // if redirect, save to redirect
  47.     {
  48.       $redirect=$v;
  49.     }
  50.   }
  51. }
  52. echo"</table>";
  53.  
  54. echo"<br><h2>info</h2>";
  55. var_dump($info);
  56.  
  57. echo"<br /><br /><br /><br />";
  58.  
  59. if($redirect!="")
  60. {
  61.   if($count>2) echo "<h1>alot of redirects, halting</h1>";
  62.   else return getContents($redirect,$count+1);
  63. }
  64. else
  65. {
  66.   echo"<h1>total $count redirects</h1>";
  67.   return $c;
  68. }
  69.  
  70. }
  71.  
  72. $c=getContents("https://marketplace.secondlife.com/p/Freebie-Tropical-Palm-Tree-with-flexi-leafs/2214529");
  73.  
  74. echo "<h1>desired content</h1>$c";
  75.  
  76. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement