/jibas/include/httprequest.php

https://github.com/wildanm/Jibas · PHP · 94 lines · 61 code · 13 blank · 20 comment · 10 complexity · 07b5a1695c485c7f55c853aa2f7f059f MD5 · raw file

  1. <?
  2. /**[N]**
  3. * JIBAS Education Community
  4. * Jaringan Informasi Bersama Antar Sekolah
  5. *
  6. * @version: 18.0 (August 01, 2019)
  7. * @notes: JIBAS Education Community will be managed by Yayasan Indonesia Membaca (http://www.indonesiamembaca.net)
  8. *
  9. * Copyright (C) 2009 Yayasan Indonesia Membaca (http://www.indonesiamembaca.net)
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation, either version 3 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. **[N]**/ ?>
  23. <?php
  24. function http_request(
  25. $verb = 'GET', /* HTTP Request Method (GET and POST supported) */
  26. $ip, /* Target IP/Hostname */
  27. $port = 80, /* Target TCP port */
  28. $uri = '/', /* Target URI */
  29. $getdata = array(), /* HTTP GET Data ie. array('var1' => 'val1', 'var2' => 'val2') */
  30. $postdata = array(), /* HTTP POST Data ie. array('var1' => 'val1', 'var2' => 'val2') */
  31. $cookie = array(), /* HTTP Cookie Data ie. array('var1' => 'val1', 'var2' => 'val2') */
  32. $custom_headers = array(), /* Custom HTTP headers ie. array('Referer: http://localhost/ */
  33. $timeout = 7000, /* Socket timeout in milliseconds */
  34. $req_hdr = false, /* Include HTTP request headers */
  35. $res_hdr = false /* Include HTTP response headers */
  36. )
  37. {
  38. $ret = '';
  39. $verb = strtoupper($verb);
  40. $cookie_str = '';
  41. $getdata_str = count($getdata) ? '?' : '';
  42. $postdata_str = '';
  43. foreach ($getdata as $k => $v)
  44. $getdata_str .= urlencode($k) .'='. urlencode($v);
  45. foreach ($postdata as $k => $v)
  46. $postdata_str .= urlencode($k) .'='. urlencode($v) .'&';
  47. foreach ($cookie as $k => $v)
  48. $cookie_str .= urlencode($k) .'='. urlencode($v) .'; ';
  49. $crlf = "\r\n";
  50. $req = $verb .' '. $uri . $getdata_str .' HTTP/1.1' . $crlf;
  51. $req .= 'Host: '. $ip . $crlf;
  52. $req .= 'User-Agent: Mozilla/5.0 Firefox/3.6.12' . $crlf;
  53. $req .= 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' . $crlf;
  54. $req .= 'Accept-Language: en-us,en;q=0.5' . $crlf;
  55. $req .= 'Accept-Encoding: deflate' . $crlf;
  56. $req .= 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7' . $crlf;
  57. foreach ($custom_headers as $k => $v)
  58. $req .= $k .': '. $v . $crlf;
  59. if (!empty($cookie_str))
  60. $req .= 'Cookie: '. substr($cookie_str, 0, -2) . $crlf;
  61. if ($verb == 'POST' && !empty($postdata_str))
  62. {
  63. $postdata_str = substr($postdata_str, 0, -1);
  64. $req .= 'Content-Type: application/x-www-form-urlencoded' . $crlf;
  65. $req .= 'Content-Length: '. strlen($postdata_str) . $crlf . $crlf;
  66. $req .= $postdata_str;
  67. }
  68. else $req .= $crlf;
  69. if ($req_hdr)
  70. $ret .= $req;
  71. if (($fp = @fsockopen($ip, $port, $errno, $errstr)) == false)
  72. return "Error $errno: $errstr\n";
  73. stream_set_timeout($fp, 0, $timeout * 1000);
  74. fputs($fp, $req);
  75. while ($line = fgets($fp)) $ret .= $line;
  76. fclose($fp);
  77. if (!$res_hdr)
  78. $ret = substr($ret, strpos($ret, "\r\n\r\n") + 4);
  79. return $ret;
  80. }
  81. ?>