HttpClient.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | A3Mall
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: xzncit <158373108@qq.com>
  8. // +----------------------------------------------------------------------
  9. namespace mall\utils;
  10. class HttpClient {
  11. public static function get($url, $query = [], $options = []){
  12. $options['query'] = $query;
  13. return self::http('get', $url, $options);
  14. }
  15. public static function post($url, $data = [], $options = []){
  16. $options['data'] = $data;
  17. return self::http('post', $url, $options);
  18. }
  19. public static function download(){}
  20. public static function http($method, $url, $options = []){
  21. $curl = curl_init();
  22. // GET参数设置
  23. if (!empty($options['query'])) {
  24. $url .= (stripos($url, '?') !== false ? '&' : '?') . http_build_query($options['query']);
  25. }
  26. // CURL头信息设置
  27. if (!empty($options['headers'])) {
  28. curl_setopt($curl, CURLOPT_HTTPHEADER, $options['headers']);
  29. }
  30. // POST数据设置
  31. if (strtolower($method) === 'post') {
  32. curl_setopt($curl, CURLOPT_POST, true);
  33. curl_setopt($curl, CURLOPT_POSTFIELDS,is_array($options['data']) ? http_build_query($options['data']) : $options['data']);
  34. }
  35. curl_setopt($curl, CURLOPT_URL, $url);
  36. curl_setopt($curl, CURLOPT_TIMEOUT, 60);
  37. curl_setopt($curl, CURLOPT_HEADER, false);
  38. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  39. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  40. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  41. list($content) = [curl_exec($curl), curl_close($curl)];
  42. return $content;
  43. }
  44. }