<?php
require_once 'AipHttpClient.php';
require_once 'AipBCEUtil.php';
class AipBase {
protected $accessTokenUrl = 'https://aip.baidubce.com/oauth/2.0/token';
protected $reportUrl = 'https://aip.baidubce.com/rpc/2.0/feedback/v1/report';
protected $appId = '';
protected $apiKey = '';
protected $secretKey = '';
protected $scope = 'brain_all_scope';
public function __construct($appId, $apiKey, $secretKey){
$this->appId = trim($appId);
$this->apiKey = trim($apiKey);
$this->secretKey = trim($secretKey);
$this->isCloudUser = null;
$this->client = new AipHttpClient();
$this->version = '2_2_2';
$this->proxies = array();
}
public function getVersion(){
return $this->version;
}
public function setConnectionTimeoutInMillis($ms){
$this->client->setConnectionTimeoutInMillis($ms);
}
public function setSocketTimeoutInMillis($ms){
$this->client->setSocketTimeoutInMillis($ms);
}
public function setProxies($proxies){
$this->client->setConf($proxies);
}
protected function proccessRequest($url, &$params, &$data, $headers){
$params['aipSdk'] = 'php';
$params['aipSdkVersion'] = $this->version;
}
protected function request($url, $data, $headers=array()){
try{
$result = $this->validate($url, $data);
if($result !== true){
return $result;
}
$params = array();
$authObj = $this->auth();
if($this->isCloudUser === false){
$params['access_token'] = $authObj['access_token'];
}
$this->proccessRequest($url, $params, $data, $headers);
$headers = $this->getAuthHeaders('POST', $url, $params, $headers);
$response = $this->client->post($url, $data, $params, $headers);
$obj = $this->proccessResult($response['content']);
if(!$this->isCloudUser && isset($obj['error_code']) && $obj['error_code'] == 110){
$authObj = $this->auth(true);
$params['access_token'] = $authObj['access_token'];
$response = $this->client->post($url, $data, $params, $headers);
$obj = $this->proccessResult($response['content']);
}
if(empty($obj) || !isset($obj['error_code'])){
$this->writeAuthObj($authObj);
}
}catch(Exception $e){
return array(
'error_code' => 'SDK108',
'error_msg' => 'connection or read data timeout',
);
}
return $obj;
}
protected function multi_request($url, $data){
try{
$params = array();
$authObj = $this->auth();
$headers = $this->getAuthHeaders('POST', $url);
if($this->isCloudUser === false){
$params['access_token'] = $authObj['access_token'];
}
$responses = $this->client->multi_post($url, $data, $params, $headers);
$is_success = false;
foreach($responses as $response){
$obj = $this->proccessResult($response['content']);
if(empty($obj) || !isset($obj['error_code'])){
$is_success = true;
}
if(!$this->isCloudUser && isset($obj['error_code']) && $obj['error_code'] == 110){
$authObj = $this->auth(true);
$params['access_token'] = $authObj['access_token'];
$responses = $this->client->post($url, $data, $params, $headers);
break;
}
}
if($is_success){
$this->writeAuthObj($authObj);
}
$objs = array();
foreach($responses as $response){
$objs[] = $this->proccessResult($response['content']);
}
}catch(Exception $e){
return array(
'error_code' => 'SDK108',
'error_msg' => 'connection or read data timeout',
);
}
return $objs;
}
protected function validate($url, &$data){
return true;
}
protected function proccessResult($content){
return json_decode($content, true);
}
private function getAuthFilePath(){
return dirname(__FILE__) . DIRECTORY_SEPARATOR . md5($this->apiKey);
}
private function writeAuthObj($obj){
if($obj === null || (isset($obj['is_read']) && $obj['is_read'] === true)){
return;
}
$obj['time'] = time();
$obj['is_cloud_user'] = $this->isCloudUser;
@file_put_contents($this->getAuthFilePath(), json_encode($obj));
}
private function readAuthObj(){
$content = @file_get_contents($this->getAuthFilePath());
if($content !== false){
$obj = json_decode($content, true);
$this->isCloudUser = $obj['is_cloud_user'];
$obj['is_read'] = true;
if($this->isCloudUser || $obj['time'] + $obj['expires_in'] - 30 > time()){
return $obj;
}
}
return null;
}
private function auth($refresh=false){
if(!$refresh){
$obj = $this->readAuthObj();
if(!empty($obj)){
return $obj;
}
}
$response = $this->client->get($this->accessTokenUrl, array(
'grant_type' => 'client_credentials',
'client_id' => $this->apiKey,
'client_secret' => $this->secretKey,
));
$obj = json_decode($response['content'], true);
$this->isCloudUser = !$this->isPermission($obj);
return $obj;
}
protected function isPermission($authObj)
{
if(empty($authObj) || !isset($authObj['scope'])){
return false;
}
$scopes = explode(' ', $authObj['scope']);
return in_array($this->scope, $scopes);
}
private function getAuthHeaders($method, $url, $params=array(), $headers=array()){
if($this->isCloudUser === false){
return $headers;
}
$obj = parse_url($url);
if(!empty($obj['query'])){
foreach(explode('&', $obj['query']) as $kv){
if(!empty($kv)){
list($k, $v) = explode('=', $kv, 2);
$params[$k] = $v;
}
}
}
$timestamp = gmdate('Y-m-d\TH:i:s\Z');
$headers['Host'] = isset($obj['port']) ? sprintf('%s:%s', $obj['host'], $obj['port']) : $obj['host'];
$headers['x-bce-date'] = $timestamp;
$headers['authorization'] = AipSampleSigner::sign(array(
'ak' => $this->apiKey,
'sk' => $this->secretKey,
), $method, $obj['path'], $headers, $params, array(
'timestamp' => $timestamp,
'headersToSign' => array_keys($headers),
));
return $headers;
}
public function report($feedback){
$data = array();
$data['feedback'] = $feedback;
return $this->request($this->reportUrl, $data);
}
}