💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
### CURL 后端访问接口 * $method参数可以指定请求方式 get 或post * $url参数可以指定要请求的地址 * $param为设置的get或者post的参数 * $timeout设置为1为异步请求,设置数值稍大则可以视为同步请求 ~~~ function curl_common($method='get', $url = '', $param = [], $header = [], $timeout =1) { $curl = curl_init(); //初始化curl curl_setopt($curl, CURLOPT_URL, $url); //抓取指定网页 curl_setopt($curl, CURLOPT_HEADER, 0); //设置header curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上 curl_setopt($curl, CURLOPT_HTTPHEADER, $header); //设置头部 curl_setopt($curl, CURLOPT_HEADER, 0); //返回头部信息 if('get' ==$method){ $url .= '?'.http_build_query($param); }else{ curl_setopt($curl, CURLOPT_POST, 1); //post提交方式 curl_setopt($curl, CURLOPT_POSTFIELDS, $param); //post提交数据 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //绕过ssl协议 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); } curl_setopt($curl, CURLOPT_TIMEOUT, $timeout); //设置超时时间 $data = curl_exec($curl); //运行curl curl_close($curl); return $data; } ~~~ ## 批量操作 ~~~ /** * * @param array $url_list * @param boolean $json_decode * @param int $time * @return array */ public static function send_post_multi($url_list, $json_decode = false, $time = 7) { $curl_multi = []; $result = []; foreach ($url_list as $i => $row) { $curl_multi[$i] = curl_init(); curl_setopt($curl_multi[$i], CURLOPT_URL, $row['http_url']); curl_setopt($curl_multi[$i], CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl_multi[$i], CURLOPT_RETURNTRANSFER, true); if (isset($row['post_data'])) { curl_setopt($curl_multi[$i], CURLOPT_POST, true); curl_setopt($curl_multi[$i], CURLOPT_POSTFIELDS, $row['post_data']); } /* isset */ if (isset($_SERVER['HTTP_USER_AGENT'])) { curl_setopt($curl_multi[$i], CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); } /* isset */ curl_setopt($curl_multi[$i], CURLOPT_TIMEOUT, $time); }/* foreach */ $mh = curl_multi_init(); foreach ($curl_multi as $ch) { curl_multi_add_handle($mh, $ch); } /* foreach */ $active = null; do { $mrc = curl_multi_exec($mh, $active); } while ($mrc == CURLM_CALL_MULTI_PERFORM); while ($active && $mrc == CURLM_OK) { if (curl_multi_select($mh) != -1) { do { $mrc = curl_multi_exec($mh, $active); } while ($mrc == CURLM_CALL_MULTI_PERFORM); } /* end */ } /* while */ foreach ($curl_multi as $i => $ch) { $result[$i] = curl_multi_getcontent($ch); if ($json_decode === true) { $result[$i] = json_decode($result[$i], true); } /* end */ curl_multi_remove_handle($mh, $ch); } /* foreach */ curl_multi_close($mh); return $result; } ~~~ ### 人性化显示某时间距离当前已经过去多久 ``` //$sTime int 时间戳 function formatTime($sTime, $formt = 'Y-m-d') { if (!$sTime) { return ''; } //sTime=源时间,cTime=当前时间,dTime=时间差 $cTime = $_SERVER['REQUEST_TIME']; $dTime = $cTime - $sTime; $dDay = intval(date('z', $cTime)) - intval(date('z', $sTime)); $dYear = intval(date('Y', $cTime)) - intval(date('Y', $sTime)); //n秒前,n分钟前,n小时前,日期 if ($dTime < 60) { if ($dTime < 10) { return '刚刚'; } else { return intval(floor($dTime / 10) * 10).'秒前'; } } elseif ($dTime < 3600) { return intval($dTime / 60).'分钟前'; } elseif ($dYear == 0 && $dTime >= 3600 && $dDay == 0) { return intval($dTime / 3600).'小时前'; } elseif ($dYear == 0 && $dDay > 0 && $dDay <= 7) { return intval($dDay).'天前'; } elseif ($dYear == 0 && $dDay > 7 && $dDay <= 30) { return intval($dDay / 7).'周前'; } elseif ($dYear == 0 && $dDay > 30) { return intval($dDay / 30).'个月前'; } elseif ($dYear == 0) { return date('m月d日', $sTime); } else { return date($formt, $sTime); } } ``` ### 统一构造的失败或成功的 JSON 响应 ~~~ // 200 成功 ----默认 // 419 有错误 // 401 没登陆 // 402 手机绑定 // 403 vip /** * @param $message * @param $data * @return JsonResponse */ public function success($message, $data = array()): JsonResponse { return response()->json([ 'code' => 200, 'message' => $message, 'data' => $data, ]); } /** * @param $message * @param $code * @return JsonResponse */ public function error($message, $code = 419):JsonResponse{ return response()->json([ 'code' => $code, 'message' => $message ],$code); } ~~~ ### 计算两点地理坐标之间的距离. ``` /** * 计算两点地理坐标之间的距离. * * @param Decimal $longitude1 起点经度 * @param Decimal $latitude1 起点纬度 * @param Decimal $longitude2 终点经度 * @param Decimal $latitude2 终点纬度 * @param int $unit 单位 1:米 2:公里 * @param int $decimal 精度 保留小数位数 * * @return Decimal */ function getDistance($longitude1, $latitude1, $longitude2, $latitude2, $unit = 1, $decimal = 2) { $EARTH_RADIUS = 6370.996; // 地球半径系数 $PI = 3.1415926; $radLat1 = $latitude1 * $PI / 180.0; $radLat2 = $latitude2 * $PI / 180.0; $radLng1 = $longitude1 * $PI / 180.0; $radLng2 = $longitude2 * $PI / 180.0; $a = $radLat1 - $radLat2; $b = $radLng1 - $radLng2; $distance = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2))); $distance = $distance * $EARTH_RADIUS * 1000; if ($unit == 2) { $distance = $distance / 1000; } return round($distance, $decimal); } ``` ~~~ /** * 判断是否微信浏览器 * @return bool */ function is_wechat() { $userAgent = strtolower($_SERVER['HTTP_USER_AGENT']); return (strpos($userAgent, 'micromessenger')) ? true : false; } ~~~ ~~~ /** * @param $response_array_data * @return string */ function aes_encode($response_array_data) { $key = '7f80d3b5c63b76c2'; $iv = '9c7a36d0d8f8a5c9'; $response_array_data = json_encode($response_array_data, JSON_UNESCAPED_UNICODE); $response_array_data = base64_encode(openssl_encrypt($response_array_data, "AES-128-CBC", $key, OPENSSL_RAW_DATA, $iv)); return $response_array_data; } ~~~ ~~~ /** * @param $response_array_data * @return string */ function aes_decode($data) { $key = '7f80d3b5c63b76c2'; $iv = '9c7a36d0d8f8a5c9'; $decode_data = openssl_decrypt(base64_decode($data), "AES-128-CBC", $key, OPENSSL_RAW_DATA, $iv); return $decode_data; } ~~~ ~~~ /** * @param $text * @return bool */ function is_zh_text($text) :bool { $pattern = "/[^\x{4E00}-\x{9FFF}]+/u"; $newStr = preg_replace($pattern,'', $text); if($newStr===''){ return false; echo "字符串中不包含中文<br>"; }else{ return true; echo "字符串中包含中文<br>"; } } ~~~ ~~~ /** * @param $text * @return bool */ function is_ug_text($text) :bool { $pattern = "/[\x{0600}-\x{06ff}]/u"; $res = preg_match($pattern,$text); if($res > 0){ return true; echo "字符串中包含维吾尔文<br>"; }else{ return false; echo "字符串中不包含维吾尔文<br>"; } } ~~~ ~~~ /** * @param $byte //传入字节单位 * @return string */ function transf_byte($byte) { //换算 $KB = 1024; $MB = $KB * 1024; $GB = $MB * 1024; $TB = $GB * 1024; if ($byte < $KB) { return $byte . 'B'; } else if ($byte < $MB) { //取两位小数四舍五入 return round($byte / $KB, 2) . 'KB'; } else if ($byte < $GB) { return round($byte / $MB, 2) . 'MB'; } else if ($byte < $TB) { return round($byte / $GB, 2) . 'GB'; } else { return round($byte / $TB, 2) . 'TB'; } } ~~~ ~~~ /** * 获取客户端IP地址 * @param integer $type 返回类型 0 返回IP地址 1 返回IPV4地址数字 * @return mixed */ function get_client_ip($type = 0) { static $ip = NULL; if ($ip !== NULL) { return $ip; } if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); $pos = array_search('unknown', $arr); if (false !== $pos) unset($arr[$pos]); $ip = trim($arr[0]); } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) { $ip = $_SERVER['HTTP_CLIENT_IP']; } elseif (isset($_SERVER['REMOTE_ADDR'])) { $ip = $_SERVER['REMOTE_ADDR']; } if (!(stripos($ip, ':') === false)) { $ip = current(explode(":", $ip)); }/*如果代理访问过滤端口*/ if ($type == 1) { $ip = sprintf("%u", ip2long($ip)); return $ip; } return $ip; } ~~~ ### php 获取文件夹中所有文件 ~~~ function getDirFile($path){ if(!is_dir($path)){ return false; } $arr = array(); $data = scandir($path); foreach ($data as $value){ if($value != '.' && $value != '..'){ $arr[] = $value; } } return $arr; } ~~~ ~~~ /** * 用户名、邮箱、手机账号中间字符串以*隐藏 * @param $str * @return mixed|string */ function string2star($str) { if (strpos($str, '@')) { $email_array = explode("@", $str); $prevfix = (strlen($email_array[0]) < 4) ? "" : substr($str, 0, 3); //邮箱前缀 $count = 0; return $prevfix . preg_replace('/([\d\w+_-]{0,100})@/', '***@', $str, -1, $count); } if (is_mobile_nomor($str)) { return substr($str, 0, 3) . "******" . substr($str, -2); } if (strlen($str) > 7) { return substr($str, 0, 3) . "****" . substr($str, -3); } $count = ceil(strlen($str) / 2 / 2); return substr($str, 0, $count) . "*****" . substr($str, -$count); } ~~~ ~~~ /** * 字符串以*隐藏 * @param string $string * @param integer $start * @param integer $end * @param integer $star * @return string */ function string2hide($string, $start = 2, $end = 2, $star = 4) { if (function_exists('mb_substr')) { return mb_substr($string, 0, $start, 'utf-8') . str_pad('*', $star, '*') . mb_substr($string, -$end, $end, 'utf-8'); } return substr($string, 0, $start) . str_pad('*', $star, '*') . mb_substr($string, -$end); } ~~~ ~~~ /** * @param $value * @return bool */ function is_mobile_nomor($value){ return preg_match('#^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[\d]{9}$|^16[6,7]{1}\d{8}$|^17[0,1,2,3,5,6,7,8]{1}\d{8}$|^18[\d]{9}$|^19[1,3,5,6,8,9]{1}\d{8}$#', $value) ? true : false; } ~~~ ~~~ /** * 过滤隐藏字符 * @param $text * @return array|string|string[] */ function filter_hidde_v2($text) { $text = str_replace("‬", "", $text); /* 结束上一次定义 */ $text = str_replace("‎", "", $text); /* 特殊字符串 */ $text = str_replace("‏", "", $text); /* 特殊字符串 */ $text = str_replace("‪", "", $text); /* 特殊字符串 */ $text = str_replace("‫", "", $text); /* 特殊字符串 */ $text = str_replace("‭", "", $text); /* 开始从左到右的文字 */ $text = str_replace("‮", "", $text); /* 开始从右到左的文字 */ $text = str_replace("", "", $text); /* 特殊字符串 */ $text = str_replace("‍", "", $text); /* 特殊字符串 */ $text = str_replace("‌", "", $text); /* 特殊字符串 */ return $text; } ~~~ ~~~ /** * 获得随机字符串 * @param integer $length 随机字符串的长度 * @param string $chars 随机字符串的内容 * @return string */ function get_rand_str($length, $chars = '0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM'){ $randStr = ''; while ($length--) { $randStr .= $chars[rand(0, strlen($chars) - 1)]; } return $randStr; } ~~~