企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## curl请求http接口 ``` <?php namespace app\index\controller; use think\Controller; class Api extends Controller { const KEY = 'aaaaaaaaaaaaaaaaaaaaaaaa'; const APPID = 'bbbbbbbbbbbbbbbbbbbbbb'; const URL = 'https://api.test.com/api/gatdata'; //输出接口(get请求) public function getData(){ //随机数 $randomStr = $this -> createNonceStr(); //接口方的appid $appid = self::APPID; //接口名 $api = 'abcd'; //生成签名 $arr = array(); $arr['nonce'] = "nonce=$randomStr"; $arr['api'] = "api=$api"; $signature = $this -> arithmetic($arr); //请求地址 $url = self::URL; //请求体 $query = [ 'api'=>$api, 'nonce'=>$randomStr, 'sign'=>$signature ]; //get $result = json_decode($this -> httpGet($url,$appid,$query),true); //返回请求数据 return json(['data'=>$result]); } //输出接口(post) public function postData(){ //随机数 $randomStr = $this -> createNonceStr(); $appid = self::APPID; $api = 'abcd'; //生成签名 $arr = array(); $arr['nonce'] = "nonce=$randomStr"; $arr['api'] = "api=$api"; $signature = $this -> arithmetic($arr); //url地址 $url = self::URL; //请求体 $body = [ 'api'=>$api, 'nonce'=>$randomStr, 'sign'=>$signature, ]; //post $result = $this -> httpPost($url,$appid,$body); return json(['data'=>$result]); } //curl模拟get请求。 private function httpGet($url,$appid,$query){ //初始化 $curl = curl_init(); $headers = array(); //微秒时间戳 $time = microtime(); list($msec, $sec) = explode(' ', $time); //毫秒时间戳 $msectime = (int)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000); //头部字段(自定义) $headers[] = "app_id:$appid"; $headers[] = "psd_entry:$msectime"; //将数组转化为字符串参数 $query = http_build_query($query); //将请求的参数拼接在url后面 $url = $url.'?'.$query; //需要请求的是哪个地址 curl_setopt($curl,CURLOPT_URL,$url); //表示把请求的数据已文件流的方式输出到变量中 curl_setopt($curl,CURLOPT_RETURNTRANSFER,1); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);//绕过ssl验证 //参数为0表示不带头文件,为1表示带头文件 curl_setopt($curl, CURLOPT_HEADER,0); //头部字段 curl_setopt($curl, CURLOPT_HTTPHEADER,$headers); $result = curl_exec($curl); curl_close($curl); return $result; } //curl模拟post请求。 private static function httpPost($url,$appid,$body){ $curl = curl_init(); $headers = array(); $time = microtime(); list($msec, $sec) = explode(' ', $time); $msectime = (int)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000); //头部字段(自定义) $headers[] = "app_id:$appid"; $headers[] = "psd_entry:$msectime"; //设置提交的url curl_setopt($curl, CURLOPT_URL, $url); //设置头文件的信息作为数据流输出 curl_setopt($curl, CURLOPT_HEADER, 1); curl_setopt($curl, CURLOPT_HTTPHEADER,$headers); //设置获取的信息以文件流的形式返回,而不是直接输出。 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //设置post方式提交 curl_setopt($curl, CURLOPT_POST, 1); //添加post数据 curl_setopt($curl, CURLOPT_POSTFIELDS, $body); //执行命令 $data = curl_exec($curl); //关闭URL请求 curl_close($curl); //获得数据并返回 return $data; } //随机生成字符串 private function createNonceStr($length = 16) { $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; $str = ""; for ($i = 0; $i < $length; $i++) { $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); } return $str; } /** * @param $timeStamp 时间戳 * @param $randomStr ASCII可见字符串 * @return string 返回签名 */ private function arithmetic($arr){ $key = self::KEY; //按照首字母大小写顺序排序 sort($arr,SORT_STRING); //拼接成字符串 $str = implode('&',$arr); //拼接key $stringSignTemp = $str.'&key='.$key; //生成签名 $sign=strtoupper(MD5($stringSignTemp)); return $sign; } } ```