PhieuLang

Curl

Sep 4th, 2012
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.24 KB | None | 0 0
  1. <?php
  2.  
  3. /*==================================
  4. Get url content and response headers (given a url, follows all redirections on it and returned content and response headers of final url)
  5.  
  6. @return    array[0]    content
  7.         array[1]    array of response headers
  8. ==================================*/
  9. function get_url( $url,  $javascript_loop = 0, $timeout = 5 )
  10. {
  11.     $url = str_replace( "&amp;", "&", urldecode(trim($url)) );
  12.  
  13.     $cookie = tempnam ("/tmp", "CURLCOOKIE");
  14.     $ch = curl_init();
  15.     curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1" );
  16.     curl_setopt( $ch, CURLOPT_URL, $url );
  17.     curl_setopt( $ch, CURLOPT_COOKIEJAR, $cookie );
  18.     curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
  19.     curl_setopt( $ch, CURLOPT_ENCODING, "" );
  20.     curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
  21.     curl_setopt( $ch, CURLOPT_AUTOREFERER, true );
  22.     curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );    # required for https urls
  23.    curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout );
  24.     curl_setopt( $ch, CURLOPT_TIMEOUT, $timeout );
  25.     curl_setopt( $ch, CURLOPT_MAXREDIRS, 10 );
  26.     $content = curl_exec( $ch );
  27.     $response = curl_getinfo( $ch );
  28.     curl_close ( $ch );
  29.  
  30.     if ($response['http_code'] == 301 || $response['http_code'] == 302)
  31.     {
  32.         ini_set("user_agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20041001 Firefox/0.10.1");
  33.  
  34.         if ( $headers = get_headers($response['url']) )
  35.         {
  36.             foreach( $headers as $value )
  37.             {
  38.                 if ( substr( strtolower($value), 0, 9 ) == "location:" )
  39.                     return get_url( trim( substr( $value, 9, strlen($value) ) ) );
  40.             }
  41.         }
  42.     }
  43.  
  44.     if (    ( preg_match("/>[[:space:]]+window\.location\.replace\('(.*)'\)/i", $content, $value) || preg_match("/>[[:space:]]+window\.location\=\"(.*)\"/i", $content, $value) ) &&
  45.             $javascript_loop < 5
  46.     )
  47.     {
  48.         return get_url( $value[1], $javascript_loop+1 );
  49.     }
  50.     else
  51.     {
  52.         return array( $content, $response );
  53.     }
  54. }
  55. $array = get_url("http://giaoduc.net.vn/");
  56. $sourcepage = htmlspecialchars($array[0],ENT_QUOTES);
  57. echo $sourcepage;
  58. ?>
Advertisement
Add Comment
Please, Sign In to add comment