/utils.php

https://github.com/gersh/raydash-php · PHP · 74 lines · 62 code · 12 blank · 0 comment · 10 complexity · 32b0bb882a67e72b01bcaee65842bcdf MD5 · raw file

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