FeieYun.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. <?php
  2. /**
  3. * @link:http://www.gdqijianshi.com/
  4. * @copyright: Copyright (c) 2020 广东七件事集团
  5. * Created by PhpStorm
  6. * Author: zal
  7. * Date: 2020-04-01
  8. * Time: 21:49
  9. */
  10. namespace common\helpers\printer\sdk;
  11. class FeieYun
  12. {
  13. // Request vars
  14. public $host;
  15. public $port;
  16. public $path;
  17. public $method;
  18. public $postdata = '';
  19. public $cookies = array();
  20. public $referer;
  21. public $accept = 'text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,image/jpeg,image/gif,*/*';
  22. public $accept_encoding = 'gzip';
  23. public $accept_language = 'en-us';
  24. public $user_agent = 'Incutio HttpClient v0.9';
  25. public $timeout = 20;
  26. public $use_gzip = true;
  27. public $persist_cookies = true;
  28. public $persist_referers = true;
  29. public $debug = false;
  30. public $handle_redirects = true;
  31. public $max_redirects = 5;
  32. public $headers_only = false;
  33. public $username;
  34. public $password;
  35. public $status;
  36. public $headers = array();
  37. public $content = '';
  38. public $errormsg;
  39. public $redirect_count = 0;
  40. public $cookie_host = '';
  41. public function __construct($host, $port = 80)
  42. {
  43. $this->host = $host;
  44. $this->port = $port;
  45. }
  46. public function get($path, $data = false)
  47. {
  48. $this->path = $path;
  49. $this->method = 'GET';
  50. if ($data) {
  51. $this->path .= '?' . $this->buildQueryString($data);
  52. }
  53. return $this->doRequest();
  54. }
  55. public function post($path, $data)
  56. {
  57. $this->path = $path;
  58. $this->method = 'POST';
  59. $this->postdata = $this->buildQueryString($data);
  60. return $this->doRequest();
  61. }
  62. public function buildQueryString($data)
  63. {
  64. $querystring = '';
  65. if (is_array($data)) {
  66. foreach ($data as $key => $val) {
  67. if (is_array($val)) {
  68. foreach ($val as $val2) {
  69. $querystring .= urlencode($key) . '=' . urlencode($val2) . '&';
  70. }
  71. } else {
  72. $querystring .= urlencode($key) . '=' . urlencode($val) . '&';
  73. }
  74. }
  75. $querystring = substr($querystring, 0, -1); // Eliminate unnecessary &
  76. } else {
  77. $querystring = $data;
  78. }
  79. return $querystring;
  80. }
  81. public function doRequest()
  82. {
  83. if (!$fp = @fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout)) {
  84. switch ($errno) {
  85. case -3:
  86. $this->errormsg = 'Socket creation failed (-3)';
  87. break;
  88. case -4:
  89. $this->errormsg = 'DNS lookup failure (-4)';
  90. break;
  91. case -5:
  92. $this->errormsg = 'Connection refused or timed out (-5)';
  93. break;
  94. default:
  95. $this->errormsg = 'Connection failed (' . $errno . ')';
  96. $this->errormsg .= ' ' . $errstr;
  97. }
  98. $this->debug($this->errormsg);
  99. return false;
  100. }
  101. socket_set_timeout($fp, $this->timeout);
  102. $request = $this->buildRequest();
  103. $this->debug('Request', $request);
  104. fwrite($fp, $request);
  105. $this->headers = array();
  106. $this->content = '';
  107. $this->errormsg = '';
  108. $inHeaders = true;
  109. $atStart = true;
  110. while (!feof($fp)) {
  111. $line = fgets($fp, 4096);
  112. if ($atStart) {
  113. $atStart = false;
  114. if (!preg_match('/HTTP\/(\\d\\.\\d)\\s*(\\d+)\\s*(.*)/', $line, $m)) {
  115. $this->errormsg = "Status code line invalid: " . htmlentities($line);
  116. $this->debug($this->errormsg);
  117. return false;
  118. }
  119. $http_version = $m[1];
  120. $this->status = $m[2];
  121. $status_string = $m[3];
  122. $this->debug(trim($line));
  123. continue;
  124. }
  125. if ($inHeaders) {
  126. if (trim($line) == '') {
  127. $inHeaders = false;
  128. $this->debug('Received Headers', $this->headers);
  129. if ($this->headers_only) {
  130. break;
  131. }
  132. continue;
  133. }
  134. if (!preg_match('/([^:]+):\\s*(.*)/', $line, $m)) {
  135. continue;
  136. }
  137. $key = strtolower(trim($m[1]));
  138. $val = trim($m[2]);
  139. if (isset($this->headers[$key])) {
  140. if (is_array($this->headers[$key])) {
  141. $this->headers[$key][] = $val;
  142. } else {
  143. $this->headers[$key] = array($this->headers[$key], $val);
  144. }
  145. } else {
  146. $this->headers[$key] = $val;
  147. }
  148. continue;
  149. }
  150. $this->content .= $line;
  151. }
  152. fclose($fp);
  153. if (isset($this->headers['content-encoding']) && $this->headers['content-encoding'] == 'gzip') {
  154. $this->debug('Content is gzip encoded, unzipping it');
  155. $this->content = substr($this->content, 10);
  156. $this->content = gzinflate($this->content);
  157. }
  158. if ($this->persist_cookies && isset($this->headers['set-cookie']) && $this->host == $this->cookie_host) {
  159. $cookies = $this->headers['set-cookie'];
  160. if (!is_array($cookies)) {
  161. $cookies = array($cookies);
  162. }
  163. foreach ($cookies as $cookie) {
  164. if (preg_match('/([^=]+)=([^;]+);/', $cookie, $m)) {
  165. $this->cookies[$m[1]] = $m[2];
  166. }
  167. }
  168. $this->cookie_host = $this->host;
  169. }
  170. if ($this->persist_referers) {
  171. $this->debug('Persisting referer: ' . $this->getRequestURL());
  172. $this->referer = $this->getRequestURL();
  173. }
  174. if ($this->handle_redirects) {
  175. if (++$this->redirect_count >= $this->max_redirects) {
  176. $this->errormsg = 'Number of redirects exceeded maximum (' . $this->max_redirects . ')';
  177. $this->debug($this->errormsg);
  178. $this->redirect_count = 0;
  179. return false;
  180. }
  181. $location = isset($this->headers['location']) ? $this->headers['location'] : '';
  182. $uri = isset($this->headers['uri']) ? $this->headers['uri'] : '';
  183. if ($location || $uri) {
  184. $url = parse_url($location . $uri);
  185. return $this->get($url['path']);
  186. }
  187. }
  188. return true;
  189. }
  190. public function buildRequest()
  191. {
  192. $headers = array();
  193. $headers[] = "{$this->method} {$this->path} HTTP/1.0";
  194. $headers[] = "Host: {$this->host}";
  195. $headers[] = "User-Agent: {$this->user_agent}";
  196. $headers[] = "Accept: {$this->accept}";
  197. if ($this->use_gzip) {
  198. $headers[] = "Accept-encoding: {$this->accept_encoding}";
  199. }
  200. $headers[] = "Accept-language: {$this->accept_language}";
  201. if ($this->referer) {
  202. $headers[] = "Referer: {$this->referer}";
  203. }
  204. if ($this->cookies) {
  205. $cookie = 'Cookie: ';
  206. foreach ($this->cookies as $key => $value) {
  207. $cookie .= "$key=$value; ";
  208. }
  209. $headers[] = $cookie;
  210. }
  211. if ($this->username && $this->password) {
  212. $headers[] = 'Authorization: BASIC ' . base64_encode($this->username . ':' . $this->password);
  213. }
  214. if ($this->postdata) {
  215. $headers[] = 'Content-Type: application/x-www-form-urlencoded';
  216. $headers[] = 'Content-Length: ' . strlen($this->postdata);
  217. }
  218. $request = implode("\r\n", $headers) . "\r\n\r\n" . $this->postdata;
  219. return $request;
  220. }
  221. public function getStatus()
  222. {
  223. return $this->status;
  224. }
  225. public function getContent()
  226. {
  227. return $this->content;
  228. }
  229. public function getHeaders()
  230. {
  231. return $this->headers;
  232. }
  233. public function getHeader($header)
  234. {
  235. $header = strtolower($header);
  236. if (isset($this->headers[$header])) {
  237. return $this->headers[$header];
  238. } else {
  239. return false;
  240. }
  241. }
  242. public function getError()
  243. {
  244. return $this->errormsg;
  245. }
  246. public function getCookies()
  247. {
  248. return $this->cookies;
  249. }
  250. public function getRequestURL()
  251. {
  252. $url = 'http://' . $this->host;
  253. if ($this->port != 80) {
  254. $url .= ':' . $this->port;
  255. }
  256. $url .= $this->path;
  257. return $url;
  258. }
  259. public function setUserAgent($string)
  260. {
  261. $this->user_agent = $string;
  262. }
  263. public function setAuthorization($username, $password)
  264. {
  265. $this->username = $username;
  266. $this->password = $password;
  267. }
  268. public function setCookies($array)
  269. {
  270. $this->cookies = $array;
  271. }
  272. public function useGzip($boolean)
  273. {
  274. $this->use_gzip = $boolean;
  275. }
  276. public function setPersistCookies($boolean)
  277. {
  278. $this->persist_cookies = $boolean;
  279. }
  280. public function setPersistReferers($boolean)
  281. {
  282. $this->persist_referers = $boolean;
  283. }
  284. public function setHandleRedirects($boolean)
  285. {
  286. $this->handle_redirects = $boolean;
  287. }
  288. public function setMaxRedirects($num)
  289. {
  290. $this->max_redirects = $num;
  291. }
  292. public function setHeadersOnly($boolean)
  293. {
  294. $this->headers_only = $boolean;
  295. }
  296. public function setDebug($boolean)
  297. {
  298. $this->debug = $boolean;
  299. }
  300. public function quickGet($url)
  301. {
  302. $bits = parse_url($url);
  303. $host = $bits['host'];
  304. $port = isset($bits['port']) ? $bits['port'] : 80;
  305. $path = isset($bits['path']) ? $bits['path'] : '/';
  306. if (isset($bits['query'])) {
  307. $path .= '?' . $bits['query'];
  308. }
  309. $client = new HttpClient($host, $port);
  310. if (!$client->get($path)) {
  311. return false;
  312. } else {
  313. return $client->getContent();
  314. }
  315. }
  316. public function quickPost($url, $data)
  317. {
  318. $bits = parse_url($url);
  319. $host = $bits['host'];
  320. $port = isset($bits['port']) ? $bits['port'] : 80;
  321. $path = isset($bits['path']) ? $bits['path'] : '/';
  322. $client = new HttpClient($host, $port);
  323. if (!$client->post($path, $data)) {
  324. return false;
  325. } else {
  326. return $client->getContent();
  327. }
  328. }
  329. public function debug($msg, $object = false)
  330. {
  331. if ($this->debug) {
  332. print '<div style="border: 1px solid red; padding: 0.5em; margin: 0.5em;"><strong>HttpClient Debug:</strong> ' . $msg;
  333. if ($object) {
  334. ob_start();
  335. print_r($object);
  336. $content = htmlentities(ob_get_contents());
  337. ob_end_clean();
  338. print '<pre>' . $content . '</pre>';
  339. }
  340. print '</div>';
  341. }
  342. }
  343. }