fix(addon/aikefu): 使用curl来发送请求
This commit is contained in:
@@ -6,10 +6,61 @@ use addon\aikefu\model\Config as KefuConfigModel;
|
|||||||
use addon\aikefu\model\Conversation as KefuConversationModel;
|
use addon\aikefu\model\Conversation as KefuConversationModel;
|
||||||
use addon\aikefu\model\Message as KefuMessageModel;
|
use addon\aikefu\model\Message as KefuMessageModel;
|
||||||
use app\api\controller\BaseApi;
|
use app\api\controller\BaseApi;
|
||||||
use extend\api\HttpClient;
|
|
||||||
|
|
||||||
class Kefu extends BaseApi
|
class Kefu extends BaseApi
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* 封装curl请求方法
|
||||||
|
* @param string $url 请求URL
|
||||||
|
* @param string $method 请求方法
|
||||||
|
* @param array $data 请求数据
|
||||||
|
* @param array $headers 请求头
|
||||||
|
* @return string 响应内容
|
||||||
|
*/
|
||||||
|
private function curlRequest($url, $method = 'GET', $data = [], $headers = [])
|
||||||
|
{
|
||||||
|
$ch = curl_init();
|
||||||
|
|
||||||
|
// 设置URL
|
||||||
|
curl_setopt($ch, CURLOPT_URL, $url);
|
||||||
|
|
||||||
|
// 设置请求方法
|
||||||
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
|
||||||
|
|
||||||
|
// 设置POST数据
|
||||||
|
if ($method === 'POST' && !empty($data)) {
|
||||||
|
curl_setopt($ch, CURLOPT_POSTFIELDS, is_array($data) ? json_encode($data) : $data);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置请求头
|
||||||
|
if (!empty($headers)) {
|
||||||
|
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 设置返回值
|
||||||
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||||
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||||||
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
||||||
|
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
|
||||||
|
|
||||||
|
// 执行请求
|
||||||
|
$response = curl_exec($ch);
|
||||||
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||||
|
|
||||||
|
// 关闭连接
|
||||||
|
curl_close($ch);
|
||||||
|
|
||||||
|
if ($response === false) {
|
||||||
|
throw new \Exception('Curl请求失败');
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($httpCode >= 400) {
|
||||||
|
throw new \Exception('HTTP请求失败,状态码:' . $httpCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $response;
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* 为事件调用初始化属性
|
* 为事件调用初始化属性
|
||||||
* @param array $data 事件数据
|
* @param array $data 事件数据
|
||||||
@@ -62,7 +113,7 @@ class Kefu extends BaseApi
|
|||||||
return $this->response($this->error('智能客服暂未启用'));
|
return $this->response($this->error('智能客服暂未启用'));
|
||||||
}
|
}
|
||||||
|
|
||||||
$config = $config_info['data']['value'];
|
$config = $config_info;
|
||||||
$apiKey = $config['api_key'];
|
$apiKey = $config['api_key'];
|
||||||
$baseUrl = $config['base_url'];
|
$baseUrl = $config['base_url'];
|
||||||
$chatEndpoint = $config['chat_endpoint'];
|
$chatEndpoint = $config['chat_endpoint'];
|
||||||
@@ -88,7 +139,7 @@ class Kefu extends BaseApi
|
|||||||
|
|
||||||
// 发送请求到Dify API
|
// 发送请求到Dify API
|
||||||
$url = $baseUrl . $chatEndpoint;
|
$url = $baseUrl . $chatEndpoint;
|
||||||
$response = HttpClient::http($url, 'POST', json_encode($requestData), $headers);
|
$response = $this->curlRequest($url, 'POST', $requestData, $headers);
|
||||||
|
|
||||||
// 解析响应
|
// 解析响应
|
||||||
$result = json_decode($response, true);
|
$result = json_decode($response, true);
|
||||||
@@ -213,7 +264,7 @@ class Kefu extends BaseApi
|
|||||||
return $this->response($this->error('智能客服暂未启用'));
|
return $this->response($this->error('智能客服暂未启用'));
|
||||||
}
|
}
|
||||||
|
|
||||||
$config = $config_info['data']['value'];
|
$config = $config_info;
|
||||||
$apiKey = $config['api_key'];
|
$apiKey = $config['api_key'];
|
||||||
$baseUrl = $config['base_url'];
|
$baseUrl = $config['base_url'];
|
||||||
|
|
||||||
@@ -231,7 +282,7 @@ class Kefu extends BaseApi
|
|||||||
|
|
||||||
// 发送请求到Dify API
|
// 发送请求到Dify API
|
||||||
$url = $baseUrl . '/conversations';
|
$url = $baseUrl . '/conversations';
|
||||||
$response = HttpClient::http($url, 'POST', json_encode($requestData), $headers);
|
$response = $this->curlRequest($url, 'POST', $requestData, $headers);
|
||||||
|
|
||||||
// 解析响应
|
// 解析响应
|
||||||
$result = json_decode($response, true);
|
$result = json_decode($response, true);
|
||||||
|
|||||||
Reference in New Issue
Block a user