ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# application/json 格式 ``` /** * post application/json * @param string $url post请求地址 * @return mixed */ function ad_curl_post($url,$params,$token) { $data_string = json_encode($params, JSON_UNESCAPED_UNICODE); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json; charset=utf-8', 'Content-Length: ' . strlen($data_string), 'Access-Token: ' . $token, ] ); $data = curl_exec($ch); curl_close($ch); return ($data); } /** * get application/json * @param string $url GET请求地址 * @return mixed */ function ad_curl_get($url,$token='') { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); if(!empty($token)){ curl_setopt($ch, CURLOPT_HTTPHEADER,['Content-Type: application/json; charset=utf-8','Access-Token: ' . $token]); }else{ curl_setopt($ch, CURLOPT_HTTPHEADER,['Content-Type: application/json; charset=utf-8']); } $data = curl_exec($ch); curl_close($ch); return ($data); } ``` # 表单请求 ``` /** * @desc:模拟表单提交 * @param $url * @param $data * @return bool|string */ function postCurl($url, array $data) { $headers = array('Content-Type: application/x-www-form-urlencoded'); $curl = curl_init(); // 启动一个CURL会话 curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址 curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查 curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在 curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求 curl_setopt($curl, CURLOPT_POSTFIELDS,$data); // Post提交的数据包 curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回 curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); $result = curl_exec($curl); // 执行操作 curl_close($curl); // 关闭CURL会话 return $result; } /** * @param string $url get请求地址 * @param int $httpCode 返回状态码 * @return mixed */ function curl_get($url, &$httpCode = 0) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //不做证书校验,部署在linux环境下请改为true curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); $file_contents = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); return $file_contents; } ```