common.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 流年 <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. // 在 application/common.php 中添加
  12. //base64 encode处理json串
  13. function base64UrlEncode($data) {
  14. // 使用 base64_encode 编码数据
  15. $encodedData = base64_encode($data);
  16. // 将 '+' 替换为 '-','/' 替换为 '_'
  17. $modifiedData = strtr($encodedData, '+/', '-_');
  18. // 不再移除等号('=')
  19. return $modifiedData;
  20. }
  21. //base64 decode处理json串
  22. function base64UrlDecode($data) {
  23. // 将 '-' 替换为 '+','_' 替换为 '/'
  24. $decodedData = strtr($data, '-_', '+/');
  25. // 使用 base64_decode 解码数据
  26. $modifiedData = base64_decode($decodedData);
  27. return $modifiedData;
  28. }
  29. //将对象转为数组
  30. function stdClassObjToArray($array) {
  31. if(is_object($array)) {
  32. $array = (array)$array;
  33. }
  34. if(is_array($array)) {
  35. foreach($array as $key=>$value) {
  36. $array[$key] = stdClassObjToArray($value);
  37. }
  38. }
  39. return $array;
  40. }
  41. //创建官网路径
  42. function commonURL(){
  43. return 'https://emoon.com';
  44. }
  45. //判断是否是手机端还是电脑端
  46. function isMobile() {
  47. // 如果有HTTP_X_WAP_PROFILE则一定是移动设备
  48. if (isset ($_SERVER['HTTP_X_WAP_PROFILE']))
  49. return true;
  50. //此条摘自TPM智能切换模板引擎,适合TPM开发
  51. if(isset ($_SERVER['HTTP_CLIENT']) &&'PhoneClient'==$_SERVER['HTTP_CLIENT'])
  52. return true;
  53. //如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
  54. if (isset ($_SERVER['HTTP_VIA']))
  55. //找不到为flase,否则为true
  56. return stristr($_SERVER['HTTP_VIA'], 'wap') ? true : false;
  57. //判断手机发送的客户端标志,兼容性有待提高
  58. if (isset ($_SERVER['HTTP_USER_AGENT'])) {
  59. $clientkeywords = array(
  60. 'nokia','sony','ericsson','mot','samsung','htc','sgh','lg','sharp','sie-','philips','panasonic','alcatel','lenovo','iphone','ipod','blackberry','meizu','android','netfront','symbian','ucweb','windowsce','palm','operamini','operamobi','openwave','nexusone','cldc','midp','wap','mobile'
  61. );
  62. //从HTTP_USER_AGENT中查找手机浏览器的关键字
  63. if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
  64. return true;
  65. }
  66. }
  67. //协议法,因为有可能不准确,放到最后判断
  68. if (isset ($_SERVER['HTTP_ACCEPT'])) {
  69. // 如果只支持wml并且不支持html那一定是移动设备
  70. // 如果支持wml和html但是wml在html之前则是移动设备
  71. if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))) {
  72. return true;
  73. }
  74. }
  75. return false;
  76. }
  77. //公共curlPost请求
  78. function curlPost($url, $data = null, $headers = [], $json = false) {
  79. $curl = curl_init($url);
  80. $options = [
  81. CURLOPT_RETURNTRANSFER => true,
  82. CURLOPT_SSL_VERIFYPEER => false,
  83. CURLOPT_SSL_VERIFYHOST => false,
  84. CURLOPT_POST => !empty($data) || $json,
  85. ];
  86. if ($json) {
  87. $headers = array_filter($headers, function($h) {
  88. return stripos($h, 'Content-Type:') === false;
  89. });
  90. $headers[] = 'Content-Type: application/json';
  91. $data = json_encode($data, JSON_UNESCAPED_UNICODE);
  92. }
  93. if (!empty($headers)) {
  94. $options[CURLOPT_HTTPHEADER] = $headers;
  95. }
  96. if (!empty($data)) {
  97. $options[CURLOPT_POSTFIELDS] = $data;
  98. }
  99. curl_setopt_array($curl, $options);
  100. $result = curl_exec($curl);
  101. curl_close($curl);
  102. return $result;
  103. }
  104. function curlPostRAG($url, $data = null, $headers = [], $json = true) {
  105. $curl = curl_init($url);
  106. $options = [
  107. CURLOPT_RETURNTRANSFER => true,
  108. CURLOPT_SSL_VERIFYPEER => false,
  109. CURLOPT_SSL_VERIFYHOST => false,
  110. CURLOPT_POST => true, // 强制启用 POST(JSON 模式下始终 POST)
  111. ];
  112. if ($json) {
  113. // 移除已有的 Content-Type 和 Accept 头,避免冲突
  114. $headers = array_filter($headers, function($h) {
  115. return stripos($h, 'Content-Type:') === false && stripos($h, 'Accept:') === false;
  116. });
  117. // 添加 JSON 专用头
  118. $headers[] = 'Content-Type: application/json';
  119. $headers[] = 'Accept: application/json';
  120. // 编码数据为 JSON(空数据编码为 "{}" 避免服务器解析错误)
  121. $data = $data !== null ? json_encode($data, JSON_UNESCAPED_UNICODE) : '{}';
  122. }
  123. // 设置请求头和数据
  124. if (!empty($headers)) {
  125. $options[CURLOPT_HTTPHEADER] = $headers;
  126. }
  127. if ($data !== null) {
  128. $options[CURLOPT_POSTFIELDS] = $data;
  129. }
  130. curl_setopt_array($curl, $options);
  131. $result = curl_exec($curl);
  132. // 错误处理(调试时启用)
  133. if ($result === false) {
  134. $error = curl_error($curl);
  135. $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  136. throw new Exception("CURL请求失败: HTTP状态码 $httpCode, 错误信息: $error");
  137. }
  138. curl_close($curl);
  139. return $result;
  140. }