Advertisement
Guest User

curl auth follow redirect

a guest
Mar 14th, 2012
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. function get_url( $url, $username = "", $password = "", $timeout = 5 )
  2. {
  3. $url = str_replace( "&", "&", urldecode(trim($url)) );
  4.  
  5. $cookie = tempnam ("/tmp", "CURLCOOKIE");
  6. $ch = curl_init();
  7. curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
  8. curl_setopt( $ch, CURLOPT_URL, $url );
  9. curl_setopt( $ch, CURLOPT_COOKIEJAR, $cookie );
  10. curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
  11. curl_setopt( $ch, CURLOPT_ENCODING, "" );
  12. curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
  13. curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
  14. curl_setopt( $ch, CURLOPT_TIMEOUT, $timeout );
  15. curl_setopt( $ch, CURLOPT_MAXREDIRS, 10 );
  16. curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true);
  17. curl_setopt( $ch, CURLOPT_USERPWD, "$username:$password");
  18. curl_setopt( $ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  19. $content = curl_exec( $ch );
  20. $response = curl_getinfo( $ch );
  21. curl_close ( $ch );
  22.  
  23. if ($response['http_code'] == 301 || $response['http_code'] == 302)
  24. {
  25. ini_set("user_agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1");
  26. $headers = get_headers($response['url']);
  27.  
  28. $location = "";
  29. foreach( $headers as $value )
  30. {
  31. if ( substr( strtolower($value), 0, 9 ) == "location:" )
  32. return get_url( trim( substr( $value, 9, strlen($value) ) ) );
  33. }
  34. }
  35.  
  36. if ( preg_match("/window\.location\.replace\('(.*)'\)/i", $content, $value) ||
  37. preg_match("/window\.location\=\"(.*)\"/i", $content, $value)
  38. )
  39. {
  40. return get_url ( $value[1] );
  41. }
  42. else
  43. {
  44. return $content;
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement