$endTime]); } /** * @title :[curl发送请求] * @param string $url 请求地址 * @param array $data 请求参数 * @param array $header 请求头 * @param string $method 请求方式 * @return bool|string * @return json|{*} * @Author : byl * @Date :2025/1/8 20:01 * @throws \Exception */ public function SendCurl(string $url, array $data = [], array $header = [], string $method = 'POST') { $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // 根据请求方法设置相应的curl选项 switch ($method) { case 'GET': if (!empty($data)) { $parsed_url = parse_url($url); if(isset($parsed_url['query'])){ $url .= '&' . http_build_query($data); }else{ $url .= '?' . http_build_query($data); } } break; case 'POST': curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); break; default: throw new \Exception("不支持的请求方法"); } // 设置请求头 $headerArr = []; foreach ($header as $key => $value) { $headerArr[] = $key . ': ' . $value; } curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArr); // 设置请求的URL curl_setopt($ch, CURLOPT_URL, $url); // 执行curl请求并返回结果 $response = curl_exec($ch); // 检查curl请求是否出错 if (curl_errno($ch)) { throw new \Exception(curl_error($ch)); } curl_close($ch); if (is_string($response)) { $response = json_decode($response, true); } Wlog('BaseDaoRui/SendCurl', [ 'url' => $url, 'data' => $data, 'header' => $header, 'method' => $method, 'response' => $response, ]); return $response; } }