DaoRuiBase.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace daorui;
  3. /**
  4. * @title:三方接口基础类
  5. * @user: zwq
  6. * @date: 2025-01-09 14:58
  7. */
  8. class DaoRuiBase
  9. {
  10. /**
  11. * 构造方法
  12. */
  13. public function __construct()
  14. {
  15. }
  16. /**
  17. * @title :[获取缓存]
  18. * @param string $field
  19. * @return array|object|json|{*}
  20. * @Author : byl
  21. * @Date :2025/1/9 9:13
  22. */
  23. protected function getCache(string $field)
  24. {
  25. $cacheData = cache($field);
  26. if ($cacheData) {
  27. // 如果为字符串则处理为数组
  28. if (is_string($cacheData)) {
  29. $cacheData = json_decode($cacheData, true);
  30. }
  31. return $cacheData;
  32. }
  33. return [];
  34. }
  35. /**
  36. * @title :[设置缓存]
  37. * @param $data 缓存数据
  38. * @param $field 缓存key
  39. * @param $endTime 过期时间
  40. * @return json|{*}
  41. * @Author : byl
  42. * @Date :2025/1/9 9:04
  43. */
  44. protected function setCache(array $data, string $field, $endTime)
  45. {
  46. cache($field, $data, ['expire' => $endTime]);
  47. }
  48. /**
  49. * @title :[curl发送请求]
  50. * @param string $url 请求地址
  51. * @param array $data 请求参数
  52. * @param array $header 请求头
  53. * @param string $method 请求方式
  54. * @return bool|string
  55. * @return json|{*}
  56. * @Author : byl
  57. * @Date :2025/1/8 20:01
  58. * @throws \Exception
  59. */
  60. public function SendCurl(string $url, array $data = [], array $header = [], string $method = 'POST')
  61. {
  62. $ch = curl_init();
  63. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  64. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  65. // 根据请求方法设置相应的curl选项
  66. switch ($method) {
  67. case 'GET':
  68. if (!empty($data)) {
  69. $parsed_url = parse_url($url);
  70. if(isset($parsed_url['query'])){
  71. $url .= '&' . http_build_query($data);
  72. }else{
  73. $url .= '?' . http_build_query($data);
  74. }
  75. }
  76. break;
  77. case 'POST':
  78. curl_setopt($ch, CURLOPT_POST, true);
  79. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
  80. break;
  81. default:
  82. throw new \Exception("不支持的请求方法");
  83. }
  84. // 设置请求头
  85. $headerArr = [];
  86. foreach ($header as $key => $value) {
  87. $headerArr[] = $key . ': ' . $value;
  88. }
  89. curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArr);
  90. // 设置请求的URL
  91. curl_setopt($ch, CURLOPT_URL, $url);
  92. // 执行curl请求并返回结果
  93. $response = curl_exec($ch);
  94. // 检查curl请求是否出错
  95. if (curl_errno($ch)) {
  96. throw new \Exception(curl_error($ch));
  97. }
  98. curl_close($ch);
  99. if (is_string($response)) {
  100. $response = json_decode($response, true);
  101. }
  102. Wlog('BaseDaoRui/SendCurl', [
  103. 'url' => $url,
  104. 'data' => $data,
  105. 'header' => $header,
  106. 'method' => $method,
  107. 'response' => $response,
  108. ]);
  109. return $response;
  110. }
  111. }