Http.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. declare(strict_types=1);
  3. namespace app\common\library;
  4. /**
  5. * Http 请求类
  6. */
  7. class Http
  8. {
  9. /**
  10. * 发送一个POST请求
  11. * @param string $url 请求URL
  12. * @param array $params 请求参数
  13. * @param array $options 扩展参数
  14. * @return mixed|string
  15. */
  16. public static function post($url, $params = [], $cookie='', $options = [])
  17. {
  18. $response = self::sendRequest($url, $params, 'POST', $cookie, $options);
  19. return $response;
  20. }
  21. /**
  22. * 发送一个GET请求
  23. * @param string $url 请求URL
  24. * @param array $params 请求参数
  25. * @param array $options 扩展参数
  26. * @return mixed|string
  27. */
  28. public static function get($url, $params = [], $cookie='', $options = [])
  29. {
  30. $response = self::sendRequest($url, $params, 'GET', $cookie, $options);
  31. return $response;
  32. }
  33. /**
  34. * CURL下载文件
  35. * @param string $url 请求的链接
  36. * @param string $savepath 保存文件的路径
  37. * @param mixed $params 传递的参数
  38. * @param mixed $options CURL的参数
  39. * @return array
  40. */
  41. public static function download($fileurl,$savepath,$options=[]){
  42. $response=new Response();
  43. if(!is_dir(dirname($savepath))){
  44. mkdir(dirname($savepath),0755,true);
  45. }
  46. $new_file = fopen($savepath, "w") or throw new \Exception("Unable to open file!");
  47. $ch = curl_init();
  48. curl_setopt($ch, CURLOPT_URL, $fileurl);
  49. curl_setopt($ch, CURLOPT_FILE, $new_file);
  50. curl_setopt($ch, CURLOPT_TIMEOUT, 60);
  51. curl_setopt_array($ch, (array)$options);
  52. $ret=curl_exec($ch);
  53. $err = curl_error($ch);
  54. if (false === $ret || !empty($err)) {
  55. $errno = curl_errno($ch);
  56. $response->set('errorCode',$errno);
  57. $response->set('errorMsg',$err);
  58. }else{
  59. $response->set('contentType',curl_getinfo($ch,CURLINFO_CONTENT_TYPE));
  60. $statusCode=curl_getinfo($ch,CURLINFO_HTTP_CODE);
  61. $response->set('statusCode',$statusCode);
  62. if($statusCode!=200) {
  63. $response->set('errorMsg','下载失败');
  64. }
  65. }
  66. curl_close($ch);
  67. fclose($new_file);
  68. return $response;
  69. }
  70. /**
  71. * CURL发送Request请求,含POST和REQUEST
  72. * @param string $url 请求的链接
  73. * @param mixed $params 传递的参数
  74. * @param string $method 请求的方法
  75. * @param mixed $options CURL的参数
  76. * @return array
  77. */
  78. public static function sendRequest($url, $params = [], $method = 'POST', $cookie='', $options = [])
  79. {
  80. $response=new Response();
  81. $method = strtoupper($method);
  82. $protocol = substr($url, 0, 5);
  83. $query_string = is_array($params) ? http_build_query($params) : $params;
  84. //echo $query_string;
  85. $ch = curl_init();
  86. $defaults = [];
  87. if ('GET' == $method) {
  88. $geturl = $query_string ? $url . (stripos($url, "?") !== false ? "&" : "?") . $query_string : $url;
  89. $defaults[CURLOPT_URL] = $geturl;
  90. } else {
  91. $defaults[CURLOPT_URL] = $url;
  92. $defaults[CURLOPT_CUSTOMREQUEST] = $method;
  93. $defaults[CURLOPT_POSTFIELDS] = $query_string;
  94. }
  95. $defaults[CURLOPT_HEADER] = false;
  96. $defaults[CURLOPT_USERAGENT] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.98 Safari/537.36";
  97. $defaults[CURLOPT_FOLLOWLOCATION] = true;
  98. $defaults[CURLOPT_RETURNTRANSFER] = true;
  99. $defaults[CURLOPT_CONNECTTIMEOUT] = 3;
  100. $defaults[CURLOPT_TIMEOUT] = 3;
  101. //$defaults[CURLOPT_ENCODING] = 'gzip,deflate,br';
  102. if ($cookie){
  103. $defaults[CURLOPT_COOKIE] = $cookie;
  104. }
  105. $options['Expect']='';
  106. curl_setopt($ch, CURLOPT_HTTPHEADER, $options);
  107. if ('https' == $protocol) {
  108. $defaults[CURLOPT_SSL_VERIFYPEER] = false;
  109. $defaults[CURLOPT_SSL_VERIFYHOST] = false;
  110. }
  111. curl_setopt_array($ch,$defaults);
  112. $ret = curl_exec($ch);
  113. $err = curl_error($ch);
  114. if (false === $ret || !empty($err)) {
  115. $errno = curl_errno($ch);
  116. $response->set('errorCode',$errno);
  117. $response->set('errorMsg',$err);
  118. }else{
  119. $response->set('contentType',curl_getinfo($ch,CURLINFO_CONTENT_TYPE));
  120. $statusCode=curl_getinfo($ch,CURLINFO_HTTP_CODE);
  121. $response->set('statusCode',$statusCode);
  122. if($statusCode==200) {
  123. $response->set('content', $ret);
  124. }else{
  125. $response->set('errorMsg',$ret);
  126. }
  127. }
  128. curl_close($ch);
  129. return $response;
  130. }
  131. }
  132. class Response
  133. {
  134. private $errorCode;
  135. private $errorMsg;
  136. private $statusCode;
  137. private $content;
  138. private $contentType;
  139. private $header;
  140. private $cookie;
  141. public function __get($name)
  142. {
  143. if($name=='content' && str_starts_with($this->contentType,'application/json')){
  144. return json_decode($this->content,true);
  145. }
  146. return $this->$name;
  147. }
  148. public function set($name,$value)
  149. {
  150. $this->$name=$value;
  151. }
  152. public function isSuccess()
  153. {
  154. return $this->errorCode===null && $this->statusCode==200;
  155. }
  156. }