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.
 
 
wastesortingserver/baidu/BaiduAiRequest.php

116 lines
5.1 KiB

<?php
require_once $_SERVER["DOCUMENT_ROOT"] . "/entity/ApiToken.php";
require_once $_SERVER["DOCUMENT_ROOT"] . "/database/DbUtil.php";
//api请求
class BaiduAiRequest
{
public static function get_token_config()
{
$config_query = new QueryParam();
DbUtil::query("select * from param where param_key in('api_key','secret_key')", $config_query);
if (!empty($config_query->getParamList())) {
$config_result = $config_query->getParamList();
if (array_key_exists("api_key", $config_result) && array_key_exists("secret_key", $config_result)) {
if ($config_result["api_key"] instanceof Param and $config_result["secret_key"] instanceof Param
and !empty($config_result["api_key"]->getParamValue()) and !empty($config_result["secret_key"]->getParamValue())) {
return array("api_key" => $config_result["api_key"]->getParamValue(), "secret_key" => $config_result["secret_key"]->getParamValue());
}
}
}
return null;
}
public static function request_token()
{
$config_result = self::get_token_config();
if (empty($config_result)) {
error("初始化配置失败,请联系管理员");
// return <<<EOF
// {"msg":"初始化配置失败,请联系管理员"}
// EOF;
return array("msg" => "初始化配置失败,请联系管理员", "status" => false);
} else {
return self::request_token_with_config($config_result["api_key"], $config_result["secret_key"]);
}
}
public static function request_token_with_config($apikey, $secret_key)
{
try {
$config_result = self::get_token_config();
if (empty($config_result)) {
error("初始化配置失败,请联系管理员");
// return <<<EOF
// {"msg":"初始化配置失败,请联系管理员"}
// EOF;
return array("msg" => "初始化配置失败,请联系管理员", "status" => false);
} else {
$post_data['grant_type'] = 'client_credentials';
$post_data['client_id'] = $apikey;
$post_data['client_secret'] = $secret_key;
$json_res = self::request_with_param("https://aip.baidubce.com/oauth/2.0/token", $post_data);
if (array_key_exists("error", $json_res)) {
return array("msg" => "百度API请求失败,请联系管理员", "status" => false, "res" => $json_res);
} else {
DbUtil::delete("delete from api_token");
$token = new ApiToken(null, $json_res["refresh_token"], $json_res["expires_in"], null, $json_res["scope"], $json_res["session_key"], $json_res["access_token"], $json_res["session_secret"]);
DbUtil::insert("insert into api_token (refresh_token, expires_in, end_time, scope, session_key, access_token, session_secret) values (?,?,?,?,?,?,?)",
$types = "sisssss", $token->getRefreshToken(), $token->getExpiresIn(), date_format(date_create(date(default_format))->add(date_interval_create_from_date_string($token->getExpiresIn() . " seconds")), default_format), $token->getScope(), $token->getSessionKey(), $token->getAccessToken(), $token->getSessionSecret());
info("token保存成功");
// return $token;
return array("msg" => "百度API请求成功", "status" => true, "token" => $token);
}
}
} catch (Exception $e) {
error($e);
// return <<<EOF
// {"msg":"百度API请求失败,请联系管理员"}
// EOF;
return array("msg" => "百度API请求失败,请联系管理员", "status" => false);
}
}
// 发送请求
public static function request_with_param($url, $param)
{
try {
$o = "";
foreach ($param as $k => $v) {
$o .= "$k=" . urlencode($v) . "&";
}
$param = substr($o, 0, -1);
if (empty($url) || empty($param)) {
return false;
}
$curlPost = $param;
$curl = curl_init($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, 512, JSON_UNESCAPED_UNICODE);
info("接口响应结果:" . $data);
return $json_res;
} catch (Exception $e) {
error($e);
return <<<EOF
{"msg":"百度API请求失败,请联系管理员"}
EOF;
}
}
}