txcloud.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. class txcloudModel extends Model
  3. {
  4. private $SecretId = '';
  5. private $SecretKey = '';
  6. //api地址[这个是人脸识别上的]
  7. public $apiurl = 'iai.tencentcloudapi.com';
  8. public function inittxCloud(){}
  9. public function initModel()
  10. {
  11. $this->option = m('option');
  12. $this->SecretId = $this->rock->jm->uncrypt($this->option->getval('txcloud_secretid'));
  13. $this->SecretKey = $this->rock->jm->uncrypt($this->option->getval('txcloud_secretkey'));
  14. $this->inittxCloud();
  15. }
  16. //创建签名和参数
  17. private function createAuth($can = array())
  18. {
  19. $barr['Action'] = '';
  20. $barr['Region'] = ''; //地区
  21. $barr['SecretId'] = $this->SecretId;
  22. $barr['Timestamp'] = time();
  23. $barr['Nonce'] = rand(10000,99999);
  24. $barr['Version'] = '2018-03-01';
  25. foreach($can as $k=>$v)$barr[$k] = $v;
  26. ksort($barr);
  27. $str = '';
  28. foreach($barr as $k=>$v)$str.='&'.$k.'='.$v.'';
  29. $str = substr($str, 1);
  30. $srcStr = 'POST'.$this->apiurl.'/?'.$str.'';
  31. $signStr = base64_encode(hash_hmac('sha1', $srcStr, $this->SecretKey, true));
  32. $barr['Signature'] = urlencode($signStr);
  33. return $barr;
  34. }
  35. /**
  36. * 发送请求 $Action方法,其他参数
  37. */
  38. public function send($Action, $can = array(), $njcan=array())
  39. {
  40. if(isempt($this->SecretId) || isempt($this->SecretKey))return returnerror('没有完整配置腾讯云api密钥');
  41. $can['Action'] = $Action;
  42. $params = $this->createAuth($can);
  43. $url = 'https://'.$this->apiurl.'';
  44. foreach($njcan as $k=>$v)$params[$k]= urlencode($v);
  45. $result = c('curl')->postjson($url, $params);
  46. if(isempt($result))return returnerror('无法访问接口');
  47. $barr = json_decode($result, true);
  48. if(!isset($barr['Response']))return returnerror('返回出错'.$result.'');
  49. $Response = $barr['Response'];
  50. if(isset($Response['Error'])){
  51. $error = $Response['Error'];
  52. return returnerror('error:'.$error['Message'].','.$error['Code'].'');
  53. }
  54. return returnsuccess($Response);
  55. }
  56. }
粤ICP备19079148号