You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
3.0 KiB
89 lines
3.0 KiB
5 years ago
|
<?php
|
||
|
|
||
|
require "ApiToken.php";
|
||
|
require "../database/DbUtil.php";
|
||
|
|
||
|
|
||
|
//api请求
|
||
|
class BaiduAiRequest
|
||
|
{
|
||
|
private static $client_id = "keaVPslHREMgVd6FwiEQqcCx";
|
||
|
private static $client_secret = "vtsDKjRVbcALDc61GAflw8UAtmGYkIPX";
|
||
|
private $url;
|
||
|
|
||
|
/**
|
||
|
* BaiduAiRequest constructor.
|
||
|
* @param $url
|
||
|
*/
|
||
|
public function __construct($url)
|
||
|
{
|
||
|
$this->url = $url;
|
||
|
}
|
||
|
|
||
|
function request()
|
||
|
{
|
||
|
try {
|
||
|
if (empty(self::$client_id) or empty(self::$client_secret)) {
|
||
|
throw new Exception("");
|
||
|
}
|
||
|
$post_data['grant_type'] = 'client_credentials';
|
||
|
$post_data['client_id'] = self::$client_id;
|
||
|
$post_data['client_secret'] = self::$client_secret;
|
||
|
$o = "";
|
||
|
foreach ($post_data as $k => $v) {
|
||
|
$o .= "$k=" . urlencode($v) . "&";
|
||
|
}
|
||
|
$param = substr($o, 0, -1);
|
||
|
if (empty($this->url) || empty($param)) {
|
||
|
return false;
|
||
|
}
|
||
|
return $this->request_with_param($param);
|
||
|
} catch (Exception $e) {
|
||
|
$GLOBALS["default_log"]->error($e);
|
||
|
return <<<EOF
|
||
|
{"msg":"百度API请求失败,请联系管理员"}
|
||
|
EOF;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 发送请求
|
||
|
function request_with_param($param)
|
||
|
{
|
||
|
|
||
|
try {
|
||
|
$curlPost = $param;
|
||
|
$curl = curl_init($this->url);//初始化curl
|
||
|
curl_setopt($curl, CURLOPT_HEADER, 0);//设置header
|
||
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
||
|
curl_setopt($curl, CURLOPT_POST, 1);//post提交方式
|
||
|
curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
|
||
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
|
||
|
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
|
||
|
$data = curl_exec($curl);//运行curl
|
||
|
curl_close($curl);
|
||
|
$json_res = json_decode($data, true);
|
||
|
info("接口响应结果:" . $data);
|
||
|
if (array_key_exists("error", $json_res)) {
|
||
|
throw new Exception("请求异常");
|
||
|
} else {
|
||
|
$token = new ApiToken($json_res["refresh_token"], $json_res["expires_in"], $json_res["session_key"], $json_res["access_token"], $json_res["scope"], $json_res["session_secret"]);
|
||
|
DbUtil::insert("insert into api_token (refresh_token, expires_in, end_time, scope, session_key, access_token, session_secret) values (?,?,?,?,?,?,?)",
|
||
|
"sisssss", $token->getRefreshToken(), $token->getExpiresIn(), $token->getEndTime(), $token->getScope(), $token->getSessionKey(), $token->getAccessToken(), $token->getSessionSecret());
|
||
|
info("token保存成功");
|
||
|
return $token;
|
||
|
}
|
||
|
} catch (Exception $e) {
|
||
|
error($e);
|
||
|
return <<<EOF
|
||
|
{"msg":"百度API请求失败,请联系管理员"}
|
||
|
EOF;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
$obj = new BaiduAiRequest("https://aip.baidubce.com/oauth/2.0/token");
|
||
|
$obj->request();
|
||
|
|
||
|
|