$timestamp, 'AppId' => config('app_id')]); foreach ($param as $v) { $allValues[] = $v; } sort($allValues, SORT_STRING); foreach ($allValues as $item) { $sign .= sprintf("%s;",$item); } $sign = md5(rtrim($sign,';;')); return $sign; } /** * 获取GET请求 * @param $url * @param array $params * @param int $timeout * @return mixed */ function curl_get($url, array $params = [], $timeout = 5) { if($params && is_array($params)){ $p=''; foreach($params as $key => $value){ $p = $p.$key.'='.$value.'&'; } if(preg_match('/\?[\d\D]+/',$url)){ $p = '&'.$p; }else if(preg_match('/\?$/',$url)){ $p = $p; }else{ $p = '?'.$p; } $p = preg_replace('/&$/','',$p); $url = $url.$p; } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); \think\Log::record(sprintf("\n[url]\t%s\n[body]\t%s",$url,json_encode($params)),'platform_api'); $result = curl_exec($ch); if($result === false) { \think\Log::record('Curl error: ' . curl_error($ch), 'platform_api'); } else { \think\Log::record('Curl success: ' . $result, 'platform_api'); } curl_close($ch); return $result; } /** * 获取POST请求 * @param $url * @param array $data * @param $timeout * @return mixed */ function curl_post($url, $data, $timeout = 5) { $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_HTTPHEADER, array( // "Content-Type: application/json; charset=utf-8", "Content-Length: " . strlen($data)) ); $res = curl_exec($ch); curl_close($ch); return $res; } /** * 返回json结果集 * @param int $code [description] 状态码 * @param string $msg [description] 提示信息 * @param array $data [description] 数据集 * @param array $to_json [description] 是否转json * @return [type] [description] json集合 */ function resultJson(int $code, string $msg, array $data = [], bool $to_json = false) { $data_arr = ['code' => $code, 'msg' => $msg, 'data' => $data]; $result = $to_json == true ? json_encode($data_arr) : $data_arr; return $result; } /** * 检测消息类型 * @param [type] $content [description] * @return [type] [description] */ function checkMsgType($content){ $content = trim($content); if(strpos($content,'img[') === 0){ return "img"; } return "text"; } /** * 获取当前网址 */ function getHost($type = 'http') { $host = $_SERVER['HTTP_HOST']; if (!$host) $host = '127.0.0.1'; return $type == 'http' ? $host : 'ws://' . $host; } /** * 获取当前图片网址 */ function getImageHost() { $host = $_SERVER['HTTP_HOST']; if (!$host) $host = '127.0.0.1'; return 'http://'.$host; } /** * 二维数组乱序 * @param $list * @return array */ function shuffle_assoc($list) { if (!is_array($list)) return $list; $keys = array_keys($list); shuffle($keys); $random = array(); foreach ($keys as $key) $random[$key] = $list[$key]; return array_column($random, null); }