PageRenderTime 36ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/startapp/start/protected/extensions/runactions/components/ERunActionsHttpClient.php

https://github.com/teeaich/snapity
PHP | 153 lines | 93 code | 25 blank | 35 comment | 13 complexity | d4e2574f712fbe36a61aa1c694035677 MD5 | raw file
  1. <?php
  2. /**
  3. * ERunActionsHttpClient.php
  4. *
  5. * A simple Http request client based on
  6. * http://www.php.net/manual/de/function.fsockopen.php#101872
  7. *
  8. * Can send url requests without waiting for response
  9. * No support for https, proxies ...
  10. *
  11. * PHP version 5.2+
  12. *
  13. * @author Joe Blocher <yii@myticket.at>
  14. * @copyright 2011 myticket it-solutions gmbh
  15. * @license New BSD License
  16. * @package runactions
  17. * @version 1.0
  18. */
  19. class ERunActionsHttpClient
  20. {
  21. public $userAgent = 'Mozilla/5.0 Firefox/3.6.12';
  22. private $_touchOnly;
  23. public function __construct($touchOnly=false)
  24. {
  25. $this->_touchOnly = $touchOnly;
  26. }
  27. /**
  28. * Socked based http request
  29. * Based on code from: http://www.php.net/manual/de/function.fsockopen.php#101872
  30. * Added touchOnly feature, changed headers
  31. *
  32. * @param mixed $ip
  33. * @param integer $port
  34. * @param string $uri
  35. * @param string $verb
  36. * @param array $getdata
  37. * @param array $postdata
  38. * @param array $cookie
  39. * @param array $custom_headers
  40. * @param integer $timeout
  41. * @param mixed $req_hdr
  42. * @param mixed $res_hdr
  43. * @return
  44. */
  45. public function request
  46. (
  47. $ip, /* Target IP/Hostname */
  48. $port = 80, /* Target TCP port */
  49. $uri = '/', /* Target URI */
  50. $verb = 'GET', /* HTTP Request Method (GET and POST supported) */
  51. $getdata = array(), /* HTTP GET Data ie. array('var1' => 'val1', 'var2' => 'val2') */
  52. $postdata = null, /* HTTP POST Data ie. array('var1' => 'val1', 'var2' => 'val2') */
  53. $contentType = 'text/plain',
  54. $cookie = array(), /* HTTP Cookie Data ie. array('var1' => 'val1', 'var2' => 'val2') */
  55. $custom_headers = array(), /* Custom HTTP headers ie. array('Referer: http://localhost/ */
  56. $timeout = 2000, /* Socket timeout in milliseconds */
  57. $req_hdr = false, /* Include HTTP request headers */
  58. $res_hdr = false /* Include HTTP response headers */
  59. )
  60. {
  61. $ret = '';
  62. $verb = strtoupper($verb);
  63. $cookie_str = '';
  64. $getdata_str = count($getdata) ? '?' : '';
  65. $postdata_str = '';
  66. foreach ($getdata as $k => $v)
  67. $getdata_str .= urlencode($k) .'='. urlencode($v);
  68. if (isset($postdata))
  69. {
  70. if (is_array($postdata))
  71. {
  72. foreach ($postdata as $k => $v)
  73. $postdata_str .= urlencode($k) .'='. urlencode($v) .'&';
  74. $postdata_str = substr($postdata_str, 0, -1);
  75. }
  76. else
  77. $postdata_str = is_string($postData) ? $postData : serialize($postData);
  78. }
  79. foreach ($cookie as $k => $v)
  80. $cookie_str .= urlencode($k) .'='. urlencode($v) .'; ';
  81. $crlf = "\r\n";
  82. $req = $verb .' '. $uri . $getdata_str .' HTTP/1.1' . $crlf;
  83. $req .= 'Host: '. $ip . $crlf;
  84. $req .= 'User-Agent: ' . $this->userAgent . $crlf;
  85. $req .= "Cache-Control: no-store, no-cache, must-revalidate" . $crlf;
  86. $req .= "Cache-Control: post-check=0, pre-check=0" . $crlf;
  87. $req .= "Pragma: no-cache" . $crlf;
  88. foreach ($custom_headers as $k => $v)
  89. $req .= $k .': '. $v . $crlf;
  90. if (!empty($cookie_str))
  91. $req .= 'Cookie: '. substr($cookie_str, 0, -2) . $crlf;
  92. if ($verb == 'POST' && !empty($postdata_str))
  93. {
  94. if (is_array($postdata))
  95. $req .= 'Content-Type: application/x-www-form-urlencoded' . $crlf;
  96. else
  97. $req .= 'Content-Type: '.$contentType . $crlf;
  98. $req .= 'Content-Length: '. strlen($postdata_str) . $crlf;
  99. $req .= 'Connection: close' . $crlf . $crlf;
  100. $req .= $postdata_str;
  101. }
  102. else
  103. $req .= 'Connection: close' . $crlf . $crlf;
  104. if ($req_hdr)
  105. $ret .= $req;
  106. if (($fp = @fsockopen($ip, $port, $errno, $errstr)) == false)
  107. {
  108. ERunActions::logError("Error $errno: $errstr");
  109. return null;
  110. }
  111. stream_set_timeout($fp, 0, $timeout * 1000);
  112. fputs($fp, $req);
  113. if (!$this->_touchOnly)
  114. {
  115. while ($line = fgets($fp)) $ret .= $line;
  116. fclose($fp);
  117. if (!$res_hdr)
  118. $ret = substr($ret, strpos($ret, "\r\n\r\n") + 4);
  119. return $ret;
  120. }
  121. else
  122. {
  123. fclose($fp);
  124. return null;
  125. }
  126. }
  127. }
  128. ?>