| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 | <?phpnamespace 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";            $resopnse = $this->httpClient->get($uri);            //$curl = new CurlService();            //$resopnse= $curl->get($uri);            $result = json_decode($resopnse->getBody()->getContents(), true);            Cache::put("baidu_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);        $access_token = $this->getAccessToken();        $resopnse = $this->httpClient->post($this->url . "rest/2.0/ocr/v1/health_code?access_token=".$access_token,['form_params' => $data]);        return json_decode($resopnse->getBody()->getContents(),true);    }    public function trip_code($image, $options=array())    {        $data = array();        $data['image'] = base64_encode($image);        $data = array_merge($data, $options);        $access_token = $this->getAccessToken();        $resopnse = $this->httpClient->post($this->url . "rest/2.0/ocr/v1/travel_card?access_token=".$access_token,['form_params' => $data]);        return json_decode($resopnse->getBody()->getContents(),true);    }}
 |