123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401 |
- <?php
- namespace addons\blog\library\aip\lib;
- use Exception;
- 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);
- }
- }
|