<?php
namespace Deepseek;
use GuzzleHttp\Client;
class Chat{
# 私密参数
private $key = 'sk-123456789';
/**
* # +========================================================================
* # | - @name 执行
* # | - @author cq <just_leaf@foxmail.com>
* # | - @copyright zmtek 2021-01-20
* # +========================================================================
*/
public function exeGuzzle($url,$body = null,$http_method = 'POST') {
if(!$url) die('url 不能为空');
$client = new client(['timeout'=>5,'verify'=> false]);
$headers = [
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . $this -> key ,
];
$presponse = $client -> post($url,['headers' => $headers,'json' => $body]);
// 响应状态
$info[] = $presponse -> getStatusCode();
//获取body找那个返回值信息
$info[] = json_decode($presponse->getBody(),true);
//获取响应头信息
$info[] = $presponse -> getHeaders();
return $info;
}
/**
* # +========================================================================
* # | - @name 执行
* # | - @author cq <just_leaf@foxmail.com>
* # | - @copyright zmtek 2021-01-20
* # +========================================================================
*/
public function exeCurl($url,$body = null,$http_method = 'POST') {
# 转化大写
$http_method = strtoupper($http_method);
if($http_method == 'POST') {
$body = json_encode($body,JSON_UNESCAPED_UNICODE);
}else{
$url = $url . '?' . http_build_query($body);
$body = null;
}
$header[] = 'Accept:application/json';
$header[] = 'Content-Type:application/json;';
$header[] = 'Authorization:Bearer ' . $this -> key;
$exc = $this -> Curl($url,$body,$http_method,$header);
return $exc;
}
/**
* # +========================================================================
* # | - @name 支持模型
* # | - @author cq <just_leaf@foxmail.com>
* # | - @copyright zmtek 2021-01-20
* # +========================================================================
*/
public function models() {
$url = 'https://api.deepseek.com/models';
$header[] = 'Accept:application/json';
$header[] = 'Content-Type:application/json;';
$header[] = 'Authorization:Bearer ' . $this -> key;
$exc = $this -> Curl($url,null,'GET',$header);
return $exc;
}
/**
* # +========================================================================
* # | - @name 余额查询
* # | - @author cq <just_leaf@foxmail.com>
* # | - @copyright zmtek 2021-01-20
* # +========================================================================
*/
public function balance() {
$url = 'https://api.deepseek.com/user/balance';
$header[] = 'Accept:application/json';
$header[] = 'Content-Type:application/json;';
$header[] = 'Authorization:Bearer ' . $this -> key;
$exc = $this -> Curl($url,null,'GET',$header);
return $exc;
}
/**
* # +========================================================================
* # | - @name 请求
* # | - @author cq <just_leaf@foxmail.com>
* # | - @copyright zmtek 2021-01-20
* # +========================================================================
*/
public function Curl($url, $data, $http_method,$header = array(), $timeout = 30)
{
# 设置
$opts = array(
CURLOPT_TIMEOUT => $timeout,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_HTTPHEADER => $header,
CURLOPT_URL => $url,
CURLOPT_HEADER => 1 ,
);
# 根据请求类型设置特定参数
if($http_method == 'POST') {
# 判断是否传输文件
$opts[CURLOPT_POST] = 1;
$opts[CURLOPT_POSTFIELDS] = $data;
}
# 初始化并执行curl请求
$ch = curl_init();
curl_setopt_array($ch, $opts);
$data = curl_exec($ch);
$error = curl_error($ch);
$httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
curl_close($ch);
# 读取body和header
list($headers, $body) = explode("\r\n\r\n", $data, 2);
$data = json_decode($body,true);
# 状态
switch($httpCode){
case 200://200 - 正常
$data['dcode'] = 1;
$data['msg'] = '一切正常';
break;
case 400://400 - 格式错误
$data['dcode'] = 0;
$data['msg'] = '格式错误';
break;
case 401://401 - 认证失败
$data['dcode'] = 0;
$data['msg'] = '认证失败';
break;
case 402://402 - 余额不足
$data['dcode'] = 0;
$data['msg'] = '余额不足';
break;
case 422://422 - 参数错误
$data['dcode'] = 0;
$data['msg'] = '参数错误';
break;
case 429://429 - 请求速率达到上限
$data['dcode'] = 0;
$data['msg'] = '请求速率达到上限';
break;
case 500://500 - 服务器故障
$data['dcode'] = 0;
$data['msg'] = '服务器故障';
break;
case 503://503 - 服务器繁忙
$data['dcode'] = 0;
$data['msg'] = '服务器繁忙';
break;
default:
$data['dcode'] = 0;
$data['msg'] = '未知错误';
}
$result['data'] = $data;
$result['httpcode'] = $httpCode;
$result['headers'] = explode("\r\n", $headers);
return $result;
}
}