Guest User

Untitled

a guest
Aug 24th, 2011
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.74 KB | None | 0 0
  1. echo http_request('GET', '91.82.85.44', '9120', '/7.html');
  2.  
  3. function http_request(
  4.     $verb = 'GET',             /* HTTP Request Method (GET and POST supported) */
  5.     $ip,                       /* Target IP/Hostname */
  6.     $port = 80,                /* Target TCP port */
  7.     $uri = '/',                /* Target URI */
  8.     $getdata = array(),        /* HTTP GET Data ie. array('var1' => 'val1', 'var2' => 'val2') */
  9.     $postdata = array(),       /* HTTP POST Data ie. array('var1' => 'val1', 'var2' => 'val2') */
  10.     $cookie = array(),         /* HTTP Cookie Data ie. array('var1' => 'val1', 'var2' => 'val2') */
  11.     $custom_headers = array(), /* Custom HTTP headers ie. array('Referer: http://localhost/ */
  12.     $timeout = 1000,           /* Socket timeout in milliseconds */
  13.     $req_hdr = false,          /* Include HTTP request headers */
  14.     $res_hdr = false           /* Include HTTP response headers */
  15.     )
  16. {
  17.     $ret = '';
  18.     $verb = strtoupper($verb);
  19.     $cookie_str = '';
  20.     $getdata_str = count($getdata) ? '?' : '';
  21.     $postdata_str = '';
  22.  
  23.     foreach ($getdata as $k => $v)
  24.                 $getdata_str .= urlencode($k) .'='. urlencode($v) . '&';
  25.  
  26.     foreach ($postdata as $k => $v)
  27.         $postdata_str .= urlencode($k) .'='. urlencode($v) .'&';
  28.  
  29.     foreach ($cookie as $k => $v)
  30.         $cookie_str .= urlencode($k) .'='. urlencode($v) .'; ';
  31.  
  32.     $crlf = "\r\n";
  33.     $req = $verb .' '. $uri . $getdata_str .' HTTP/1.1' . $crlf;
  34.     $req .= 'Host: '. $ip . $crlf;
  35.     $req .= 'User-Agent: Mozilla/5.0 Firefox/3.6.12' . $crlf;
  36.     $req .= 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' . $crlf;
  37.     $req .= 'Accept-Language: en-us,en;q=0.5' . $crlf;
  38.     $req .= 'Accept-Encoding: deflate' . $crlf;
  39.     $req .= 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7' . $crlf;
  40.    
  41.     foreach ($custom_headers as $k => $v)
  42.         $req .= $k .': '. $v . $crlf;
  43.        
  44.     if (!empty($cookie_str))
  45.         $req .= 'Cookie: '. substr($cookie_str, 0, -2) . $crlf;
  46.        
  47.     if ($verb == 'POST' && !empty($postdata_str))
  48.     {
  49.         $postdata_str = substr($postdata_str, 0, -1);
  50.         $req .= 'Content-Type: application/x-www-form-urlencoded' . $crlf;
  51.         $req .= 'Content-Length: '. strlen($postdata_str) . $crlf . $crlf;
  52.         $req .= $postdata_str;
  53.     }
  54.     else $req .= $crlf;
  55.    
  56.     if ($req_hdr)
  57.         $ret .= $req;
  58.    
  59.     if (($fp = @fsockopen($ip, $port, $errno, $errstr)) == false)
  60.         return "Error $errno: $errstr\n";
  61.    
  62.     stream_set_timeout($fp, 0, $timeout * 1000);
  63.    
  64.     fputs($fp, $req);
  65.     while ($line = fgets($fp)) $ret .= $line;
  66.     fclose($fp);
  67.    
  68.     if (!$res_hdr)
  69.         $ret = substr($ret, strpos($ret, "\r\n\r\n") + 4);
  70.    
  71.     return $ret;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment