common.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. // 应用公共函数库文件
  13. /**
  14. * 获取当前访问的渠道(微信小程序、H5、APP等)
  15. * @return string|null
  16. */
  17. function getPlatform(): ?string
  18. {
  19. return 'H5';
  20. static $value = null;
  21. // 从header中获取 channel
  22. empty($value) && $value = request()->header('platform');
  23. // 调试模式下可通过param中获取
  24. if (is_debug() && empty($value)) {
  25. $value = request()->param('platform');
  26. }
  27. return $value;
  28. }
  29. /**
  30. * 使用curl函数库发送请求
  31. */
  32. function curl_request($url, $method = 'get', $headers = [], $params = [], $type = 'http')
  33. {
  34. /**
  35. * 初始化curl,返回资源
  36. */
  37. $curl = curl_init($url);
  38. /**
  39. * 默认是get请求,如果是post/put请求,设置请求方式和请求参数
  40. */
  41. if (strtoupper($method) == 'POST') {
  42. curl_setopt($curl, CURLOPT_POST, true);
  43. curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
  44. }
  45. if (strtoupper($method) == 'PUT') {
  46. curl_setopt($curl, CURLOPT_PUT, true);
  47. }
  48. if (!empty($headers)) {
  49. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  50. }
  51. /**
  52. * 如果是HTTPS协议,禁止从服务器验证本地证书
  53. */
  54. if (strtoupper($type) == 'HTTPS') {
  55. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  56. }
  57. /**
  58. * 发送请求,返回结果
  59. */
  60. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  61. /**
  62. * 选择地址
  63. * curl_setopt($curl,CURLOPT_URL,$url);
  64. */
  65. $res = curl_exec($curl);
  66. /**
  67. * 关闭请求
  68. */
  69. curl_close($curl);
  70. return $res;
  71. }