Http.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * Http.php UTF-8
  4. * 基于CURL的Http请求类
  5. *
  6. * @date : 2018/4/25 12:18
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huolib\tool;
  13. use think\Exception;
  14. class Http {
  15. private static function init($params = null) {
  16. if (function_exists('curl_init')) {
  17. $ch = curl_init();
  18. curl_setopt($ch, CURLOPT_HEADER, false);
  19. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  20. if (null !== $params) {
  21. self::setParams($ch, $params);
  22. }
  23. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  24. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  25. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  26. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  27. return $ch;
  28. } else {
  29. throw E('服务器不支持CURL');
  30. }
  31. }
  32. private static function setParams($ch, $params) {
  33. if (array_key_exists('header', $params)) {
  34. curl_setopt($ch, CURLOPT_HTTPHEADER, $params['header']);
  35. }
  36. curl_setopt($ch, CURLOPT_TIMEOUT, array_key_exists('timeout', $params) ? $params['timeout'] : 30);
  37. }
  38. /**
  39. * @param $ch
  40. *
  41. * @return mixed
  42. * @throws Exception
  43. */
  44. private static function exec($ch) {
  45. $rsp = curl_exec($ch);
  46. if ($rsp !== false) {
  47. curl_close($ch);
  48. return $rsp;
  49. } else {
  50. $errorCode = curl_errno($ch);
  51. $errorMsg = curl_error($ch);
  52. curl_close($ch);
  53. throw new Exception("curl出错,$errorMsg", $errorCode);
  54. }
  55. }
  56. public static function request($url, $data = null, $method = 'get', $params = null) {
  57. $method = strtolower($method);
  58. return self::$method($url, $data, $params);
  59. }
  60. public static function get($url, $data = null, $params = null) {
  61. $ch = self::init($params);
  62. $url = rtrim($url, '?');
  63. if (!is_null($data)) {
  64. $url .= '?'.http_build_query($data);
  65. }
  66. curl_setopt($ch, CURLOPT_URL, $url);
  67. return self::exec($ch);
  68. }
  69. public static function post($url, $data = null, $params = null) {
  70. $ch = self::init($params);
  71. curl_setopt($ch, CURLOPT_URL, $url);
  72. curl_setopt($ch, CURLOPT_POST, true);
  73. if (!is_null($data)) {
  74. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
  75. }
  76. return self::exec($ch);
  77. }
  78. public static function postRaw($url, $raw, $params = null) {
  79. $ch = self::init($params);
  80. curl_setopt($ch, CURLOPT_URL, $url);
  81. curl_setopt($ch, CURLOPT_POST, true);
  82. curl_setopt($ch, CURLOPT_POSTFIELDS, $raw);
  83. return self::exec($ch);
  84. }
  85. /**
  86. * @param $url
  87. * @param $raw
  88. * @param null $params
  89. *
  90. * @return mixed
  91. * @throws Exception
  92. */
  93. public static function postRawSsl($url, $raw, $params = null) {
  94. $ch = self::init($params);
  95. if (!array_key_exists('cert_path', $params) || !array_key_exists('key_path', $params)
  96. || !array_key_exists(
  97. 'ca_path', $params
  98. )) {
  99. throw new Exception('证书文件路径不能为空');
  100. }
  101. curl_setopt($ch, CURLOPT_SSLCERT, $params['cert_path']);
  102. curl_setopt($ch, CURLOPT_SSLKEY, $params['key_path']);
  103. curl_setopt($ch, CURLOPT_CAINFO, $params['ca_path']);
  104. curl_setopt($ch, CURLOPT_URL, $url);
  105. curl_setopt($ch, CURLOPT_POST, true);
  106. curl_setopt($ch, CURLOPT_POSTFIELDS, $raw);
  107. return self::exec($ch);
  108. }
  109. /**
  110. * @param $url
  111. * @param $path
  112. * @param null $filename
  113. * @param null $params
  114. *
  115. * @return bool|int
  116. * @throws Exception
  117. */
  118. public static function saveImage($url, $path, $filename = null, $params = null) {
  119. $ch = self::init($params);
  120. curl_setopt($ch, CURLOPT_URL, $url);
  121. $img = curl_exec($ch);
  122. if ($img !== false) {
  123. $file_info = curl_getinfo($ch);
  124. curl_close($ch);
  125. } else {
  126. $errorCode = curl_errno($ch);
  127. $errorMsg = curl_error($ch);
  128. curl_close($ch);
  129. throw new Exception("获取头像出错,$errorMsg", $errorCode);
  130. }
  131. $content_type = explode('/', $file_info['content_type']);
  132. if (strtolower($content_type[0]) != 'image') {
  133. throw new Exception('下载地址文件不是图片');
  134. }
  135. $file_path = '/'.trim($path, '/').'/';
  136. if (is_null($filename)) {
  137. $filename = md5($url);
  138. }
  139. $file_path .= $filename.'.'.end($content_type);
  140. return file_put_contents($file_path, $img);
  141. }
  142. public static function postJson($url, $data = [], $params = null) {
  143. $ch = self::init($params);
  144. curl_setopt($ch, CURLOPT_URL, $url);
  145. curl_setopt($ch, CURLOPT_POST, true);
  146. if (!is_null($data)) {
  147. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
  148. }
  149. return self::exec($ch);
  150. }
  151. }