ApiBase.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. namespace App\Services\Vendor\BaiduAi;
  3. class ApiBase
  4. {
  5. /**
  6. * 获取access token url
  7. * @var string
  8. */
  9. protected $accessTokenUrl = 'https://aip.baidubce.com/oauth/2.0/token';
  10. /**
  11. * 反馈接口
  12. * @var string
  13. */
  14. protected $reportUrl = 'https://aip.baidubce.com/rpc/2.0/feedback/v1/report';
  15. /**
  16. * appId
  17. * @var string
  18. */
  19. protected $appId = '';
  20. /**
  21. * apiKey
  22. * @var string
  23. */
  24. protected $apiKey = '';
  25. /**
  26. * secretKey
  27. * @var string
  28. */
  29. protected $secretKey = '';
  30. /**
  31. * 权限
  32. * @var array
  33. */
  34. protected $scope = 'brain_all_scope';
  35. protected $version = "2_2_4";
  36. protected $isCloudUser = null;
  37. protected $client;
  38. public function __construct(string $appId, string $apiKey, string $secretKey)
  39. {
  40. $this->appId = $appId;
  41. $this->apiKey = $apiKey;
  42. $this->secretKey = $secretKey;
  43. $this->client = new Request();
  44. }
  45. /**
  46. * 查看版本
  47. * @return string
  48. *
  49. */
  50. public function getVersion()
  51. {
  52. return $this->version;
  53. }
  54. /**
  55. * 反馈
  56. *
  57. * @param $feedback
  58. * @return array
  59. */
  60. public function report($feedback)
  61. {
  62. $data = array();
  63. $data['feedback'] = $feedback;
  64. return $this->request($this->reportUrl, $data);
  65. }
  66. /**
  67. * Api 请求
  68. * @param string $url
  69. * @param mixed $data
  70. * @param array $headers
  71. * @return mixed
  72. */
  73. public function request($url, $data, $headers = array())
  74. {
  75. try {
  76. if (is_array($data)) {
  77. $data = json_encode($data);
  78. }
  79. $result = $this->validate($url, $data);
  80. if ($result !== true) {
  81. return $result;
  82. }
  83. $params = array();
  84. $authObj = $this->auth();
  85. if ($this->isCloudUser === false) {
  86. $params['access_token'] = $authObj['access_token'];
  87. }
  88. // 特殊处理
  89. $this->proccessRequest($url, $params, $data, $headers);
  90. $obj = $this->client->post($url, $data, $params, $headers);
  91. if (!$this->isCloudUser && isset($obj['error_code']) && $obj['error_code'] == 110) {
  92. $authObj = $this->auth();
  93. $params['access_token'] = $authObj['access_token'];
  94. $obj = $this->client->post($url, $data, $params, $headers);
  95. }
  96. } catch (\Exception $e) {
  97. return array(
  98. 'error_code' => 'SDK108',
  99. 'error_msg' => 'connection or read data timeout',
  100. );
  101. }
  102. return $obj;
  103. }
  104. /**
  105. * 格式检查
  106. * @param string $url
  107. * @param array $data
  108. * @return bool
  109. */
  110. protected function validate($url, &$data)
  111. {
  112. return true;
  113. }
  114. /**
  115. * 认证
  116. * @return array
  117. */
  118. private function auth()
  119. {
  120. $obj = $this->client->get($this->accessTokenUrl, array(
  121. 'grant_type' => 'client_credentials',
  122. 'client_id' => $this->apiKey,
  123. 'client_secret' => $this->secretKey,
  124. ));
  125. $this->isCloudUser = !$this->isPermission($obj);
  126. return $obj;
  127. }
  128. /**
  129. * 判断认证是否有权限
  130. * @param array $authObj
  131. * @return boolean
  132. */
  133. protected function isPermission($authObj)
  134. {
  135. if (empty($authObj) || !isset($authObj['scope'])) {
  136. return false;
  137. }
  138. $scopes = explode(' ', $authObj['scope']);
  139. return in_array($this->scope, $scopes);
  140. }
  141. /**
  142. * 处理请求参数
  143. * @param string $url
  144. * @param array $params
  145. * @param array $data
  146. * @param array $headers
  147. */
  148. protected function proccessRequest($url, &$params, &$data, $headers)
  149. {
  150. $params['aipSdk'] = 'php';
  151. $params['aipSdkVersion'] = $this->version;
  152. }
  153. /**
  154. * 通用接口
  155. * @param string $url
  156. * @param array $data
  157. * @param array header
  158. * @return array
  159. */
  160. public function post($url, $data, $headers = array())
  161. {
  162. return $this->request($url, $data, $headers);
  163. }
  164. /**
  165. * @param string $method HTTP method
  166. * @param string $url
  167. * @param array $params
  168. * @param array $headers
  169. * @return array
  170. */
  171. private function getAuthHeaders($method, $url, $params = array(), $headers = array())
  172. {
  173. //不是云的老用户则不用在header中签名 认证
  174. if ($this->isCloudUser === false) {
  175. return $headers;
  176. }
  177. }
  178. }