Browse Source

增加百度接口

sandm 2 years ago
parent
commit
a1ee8b14d5
1 changed files with 66 additions and 0 deletions
  1. 66 0
      app/Services/Common/BaiduService.php

+ 66 - 0
app/Services/Common/BaiduService.php

@@ -0,0 +1,66 @@
+<?php
+
+namespace App\Services\Common;
+
+use GuzzleHttp\Client;
+use Illuminate\Support\Facades\Cache;
+
+/**
+ * 百度智能云
+ * Class BaiduService
+ * @package App\Services\Common
+ */
+class BaiduService
+{
+
+    protected $url = "https://aip.baidubce.com/";
+
+    protected $appid;
+
+    protected $appkey;
+
+    protected $httpClient;
+
+    /**
+     * BaiduService constructor.
+     */
+    public function __construct()
+    {
+        $this->httpClient=new Client([
+            'http_errors' => false
+        ]);
+
+        $this->appid=config('aix.fangyi.fangyi_zhaokao.fangyi_zhaokao.client_id');
+        $this->appkey=config('aix.fangyi.fangyi_zhaokao.fangyi_zhaokao.client_secret');
+
+    }
+
+    public function getAccessToken()
+    {
+        $access_token = Cache::get('baidu_access_token');
+        if (empty($access_token)) {
+            $uri=$this->url."oauth/2.0/token?grant_type=client_credentials&client_id=$this->appid&client_secret=$this->appkey";
+            $curl = new CurlService();
+            $resopnse= $curl->get($uri);
+            $result = json_decode($resopnse, true);
+            Cache::put("wechat_access_token", $result['access_token'], 24*60*29);
+            return $result['access_token'];
+        }
+        return $access_token;
+    }
+
+    public function health_code($image, $options=array())
+    {
+        $data = array();
+
+        $data['image'] = base64_encode($image);
+
+        $data = array_merge($data, $options);
+
+        $curl = new CurlService();
+
+        $resopnse = $this->httpClient->post($this->url . "rest/2.0/ocr/v1/health_code",['form_params' => $data]);
+
+        dd($resopnse->getBody()->getContents());
+    }
+}