| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- <?php
- // +----------------------------------------------------------------------
- // | ThinkPHP [ WE CAN DO IT JUST THINK ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
- // +----------------------------------------------------------------------
- // | Author: 流年 <liu21st@gmail.com>
- // +----------------------------------------------------------------------
- // 在 application/common.php 中添加
- //base64 encode处理json串
- function base64UrlEncode($data) {
- // 使用 base64_encode 编码数据
- $encodedData = base64_encode($data);
- // 将 '+' 替换为 '-','/' 替换为 '_'
- $modifiedData = strtr($encodedData, '+/', '-_');
- // 不再移除等号('=')
- return $modifiedData;
- }
-
- //base64 decode处理json串
- function base64UrlDecode($data) {
- // 将 '-' 替换为 '+','_' 替换为 '/'
- $decodedData = strtr($data, '-_', '+/');
- // 使用 base64_decode 解码数据
- $modifiedData = base64_decode($decodedData);
- return $modifiedData;
- }
-
- //将对象转为数组
- function stdClassObjToArray($array) {
- if(is_object($array)) {
- $array = (array)$array;
- }
- if(is_array($array)) {
- foreach($array as $key=>$value) {
- $array[$key] = stdClassObjToArray($value);
- }
- }
- return $array;
- }
- //创建官网路径
- function commonURL(){
- return 'https://emoon.com';
- }
- //判断是否是手机端还是电脑端
- function isMobile() {
- // 如果有HTTP_X_WAP_PROFILE则一定是移动设备
- if (isset ($_SERVER['HTTP_X_WAP_PROFILE']))
- return true;
-
- //此条摘自TPM智能切换模板引擎,适合TPM开发
- if(isset ($_SERVER['HTTP_CLIENT']) &&'PhoneClient'==$_SERVER['HTTP_CLIENT'])
- return true;
- //如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
- if (isset ($_SERVER['HTTP_VIA']))
- //找不到为flase,否则为true
- return stristr($_SERVER['HTTP_VIA'], 'wap') ? true : false;
- //判断手机发送的客户端标志,兼容性有待提高
- if (isset ($_SERVER['HTTP_USER_AGENT'])) {
- $clientkeywords = array(
- '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'
- );
- //从HTTP_USER_AGENT中查找手机浏览器的关键字
- if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
- return true;
- }
- }
- //协议法,因为有可能不准确,放到最后判断
- if (isset ($_SERVER['HTTP_ACCEPT'])) {
- // 如果只支持wml并且不支持html那一定是移动设备
- // 如果支持wml和html但是wml在html之前则是移动设备
- 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')))) {
- return true;
- }
- }
- return false;
- }
-
- //公共curlPost请求
- function curlPost($url, $data = null, $headers = [], $json = false) {
- $curl = curl_init($url);
- $options = [
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_SSL_VERIFYPEER => false,
- CURLOPT_SSL_VERIFYHOST => false,
- CURLOPT_POST => !empty($data) || $json,
- ];
- if ($json) {
- $headers = array_filter($headers, function($h) {
- return stripos($h, 'Content-Type:') === false;
- });
- $headers[] = 'Content-Type: application/json';
- $data = json_encode($data, JSON_UNESCAPED_UNICODE);
- }
- if (!empty($headers)) {
- $options[CURLOPT_HTTPHEADER] = $headers;
- }
- if (!empty($data)) {
- $options[CURLOPT_POSTFIELDS] = $data;
- }
- curl_setopt_array($curl, $options);
- $result = curl_exec($curl);
- curl_close($curl);
- return $result;
- }
-
- function curlPostRAG($url, $data = null, $headers = [], $json = true) {
- $curl = curl_init($url);
- $options = [
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_SSL_VERIFYPEER => false,
- CURLOPT_SSL_VERIFYHOST => false,
- CURLOPT_POST => true, // 强制启用 POST(JSON 模式下始终 POST)
- ];
-
- if ($json) {
- // 移除已有的 Content-Type 和 Accept 头,避免冲突
- $headers = array_filter($headers, function($h) {
- return stripos($h, 'Content-Type:') === false && stripos($h, 'Accept:') === false;
- });
- // 添加 JSON 专用头
- $headers[] = 'Content-Type: application/json';
- $headers[] = 'Accept: application/json';
- // 编码数据为 JSON(空数据编码为 "{}" 避免服务器解析错误)
- $data = $data !== null ? json_encode($data, JSON_UNESCAPED_UNICODE) : '{}';
- }
-
- // 设置请求头和数据
- if (!empty($headers)) {
- $options[CURLOPT_HTTPHEADER] = $headers;
- }
- if ($data !== null) {
- $options[CURLOPT_POSTFIELDS] = $data;
- }
-
- curl_setopt_array($curl, $options);
- $result = curl_exec($curl);
-
- // 错误处理(调试时启用)
- if ($result === false) {
- $error = curl_error($curl);
- $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
- throw new Exception("CURL请求失败: HTTP状态码 $httpCode, 错误信息: $error");
- }
-
- curl_close($curl);
- return $result;
- }
|