BaiduService.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace App\Services\Common;
  3. use GuzzleHttp\Client;
  4. use Illuminate\Support\Facades\Cache;
  5. /**
  6. * 百度智能云
  7. * Class BaiduService
  8. * @package App\Services\Common
  9. */
  10. class BaiduService
  11. {
  12. protected $url = "https://aip.baidubce.com/";
  13. protected $appid;
  14. protected $appkey;
  15. protected $httpClient;
  16. /**
  17. * BaiduService constructor.
  18. */
  19. public function __construct()
  20. {
  21. $this->httpClient=new Client([
  22. 'http_errors' => false
  23. ]);
  24. $this->appid=config('aix.fangyi.fangyi_zhaokao.fangyi_zhaokao.client_id');
  25. $this->appkey=config('aix.fangyi.fangyi_zhaokao.fangyi_zhaokao.client_secret');
  26. }
  27. public function getAccessToken()
  28. {
  29. $access_token = Cache::get('baidu_access_token');
  30. if (empty($access_token)) {
  31. $uri=$this->url."oauth/2.0/token?grant_type=client_credentials&client_id=$this->appid&client_secret=$this->appkey";
  32. $curl = new CurlService();
  33. $resopnse= $curl->get($uri);
  34. $result = json_decode($resopnse, true);
  35. Cache::put("wechat_access_token", $result['access_token'], 24*60*29);
  36. return $result['access_token'];
  37. }
  38. return $access_token;
  39. }
  40. public function health_code($image, $options=array())
  41. {
  42. $data = array();
  43. $data['image'] = base64_encode($image);
  44. $data = array_merge($data, $options);
  45. $curl = new CurlService();
  46. $resopnse = $this->httpClient->post($this->url . "rest/2.0/ocr/v1/health_code",['form_params' => $data]);
  47. dd($resopnse->getBody()->getContents());
  48. }
  49. }