Http.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 桌子工作室 [Http协议] V0.12015014 Released
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2015-2016 ,MrTable All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: Mr Table <admin@hjf.pw>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace library\utils;
  13. class Http{
  14. private static $boundary = '';
  15. public static $cookie = [];
  16. /**
  17. * get获取
  18. * @param string $url
  19. * @param type $params 数组类型
  20. * @return type
  21. */
  22. public static function get($url, $params = '',$cookie = [])
  23. {
  24. if(is_array($params)) $url = $url . '?' . http_build_query($params);
  25. return self::https($url, 'GET',null,[],$cookie);
  26. }
  27. /**
  28. * post传递
  29. * @param type $url
  30. * @param type $params post数组
  31. * @param type $files 文件数据
  32. * @return type
  33. */
  34. public static function post($url, $params, $files = array(),$headers = array(),$cookie = [])
  35. {
  36. if (!$files) {
  37. $body = http_build_query($params);
  38. } else {
  39. $body = self::build_http_query_multi($params, $files);
  40. $headers[] = "Content-Type: multipart/form-data; boundary=" . self::$boundary;
  41. }
  42. return self::https($url, 'POST', $body, $headers,$cookie);
  43. }
  44. /**
  45. * 获取文件数据
  46. * @param type $durl
  47. * @return type
  48. */
  49. static function file_get_contents($durl) {
  50. $ch = curl_init();
  51. curl_setopt($ch, CURLOPT_URL, $durl);
  52. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  53. // curl_setopt($ch, CURLOPT_USERAGENT, _USERAGENT_);
  54. // curl_setopt($ch, CURLOPT_REFERER, _REFERER_);
  55. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  56. $r = curl_exec($ch);
  57. curl_close($ch);
  58. return $r;
  59. }
  60. /**
  61. * 传递json值
  62. * @param $url
  63. * @param $json
  64. */
  65. public static function Json($url,$json){
  66. $jsonStr = $json;
  67. $headers = array(
  68. 'Content-Type: application/json; charset=utf-8',
  69. 'Content-Length: ' . strlen($jsonStr)
  70. );
  71. return self::https($url, 'json', $jsonStr, $headers);
  72. }
  73. static function join_cookie($cookie) {
  74. foreach( $cookie as $k=> $v){
  75. $d[] =$k."=".$v;
  76. }
  77. $data = implode(";",$d);
  78. return $data;
  79. }
  80. private static function https($url, $method, $postfields = NULL, $headers = array(),$cookie = [])
  81. {
  82. $ci = curl_init();
  83. /* Curl settings */
  84. curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
  85. curl_setopt($ci, CURLOPT_USERAGENT, 'X-YZ-Client 2.0.0 - PHP');
  86. curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);
  87. curl_setopt($ci, CURLOPT_TIMEOUT, 30);
  88. curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
  89. curl_setopt($ci, CURLOPT_ENCODING, "");
  90. curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false);
  91. curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, 2);
  92. curl_setopt($ci, CURLOPT_HEADER, FALSE);
  93. switch ($method) {
  94. case 'POST':
  95. curl_setopt($ci, CURLOPT_POST, TRUE);
  96. if (!empty($postfields)) {
  97. curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
  98. }
  99. break;
  100. case 'json':
  101. curl_setopt($ci, CURLOPT_POST, TRUE);
  102. curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
  103. break;
  104. }
  105. if(count($cookie) > 0) {
  106. curl_setopt($ci, CURLOPT_COOKIE, self::join_cookie($cookie));
  107. }
  108. curl_setopt($ci, CURLOPT_URL, $url);
  109. curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
  110. curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE);
  111. $response = curl_exec($ci);
  112. $httpCode = curl_getinfo($ci, CURLINFO_HTTP_CODE);
  113. $httpInfo = curl_getinfo($ci);
  114. curl_close($ci);
  115. return $response;
  116. }
  117. private static function build_http_query_multi($params, $files)
  118. {
  119. if (!$params) return '';
  120. $pairs = array();
  121. self::$boundary = $boundary = uniqid('------------------');
  122. $MPboundary = '--' . $boundary;
  123. $endMPboundary = $MPboundary . '--';
  124. $multipartbody = '';
  125. foreach ($params as $key => $value) {
  126. $multipartbody .= $MPboundary . "\r\n";
  127. $multipartbody .= 'content-disposition: form-data; name="' . $key . "\"\r\n\r\n";
  128. $multipartbody .= $value . "\r\n";
  129. }
  130. foreach ($files as $key => $value) {
  131. if (!$value) {
  132. continue;
  133. }
  134. if (is_array($value)) {
  135. $url = $value['url'];
  136. if (isset($value['name'])) {
  137. $filename = $value['name'];
  138. } else {
  139. $parts = explode('?', basename($value['url']));
  140. $filename = $parts[0];
  141. }
  142. $field = isset($value['field']) ? $value['field'] : $key;
  143. } else {
  144. $url = $value;
  145. $parts = explode('?', basename($url));
  146. $filename = $parts[0];
  147. $field = $key;
  148. }
  149. $content = file_get_contents($url);
  150. $multipartbody .= $MPboundary . "\r\n";
  151. $multipartbody .= 'Content-Disposition: form-data; name="' . $field . '"; filename="' . $filename . '"' . "\r\n";
  152. $multipartbody .= "Content-Type: image/unknown\r\n\r\n";
  153. $multipartbody .= $content . "\r\n";
  154. }
  155. $multipartbody .= $endMPboundary;
  156. return $multipartbody;
  157. }
  158. }