first commit
This commit is contained in:
1
金壶/ydyd_service/extend/BaiduOcr/lib/.pydio
Normal file
1
金壶/ydyd_service/extend/BaiduOcr/lib/.pydio
Normal file
@@ -0,0 +1 @@
|
||||
67e6fe11-3667-462b-947e-f5d4d9fe918a
|
||||
345
金壶/ydyd_service/extend/BaiduOcr/lib/AipBCEUtil.php
Normal file
345
金壶/ydyd_service/extend/BaiduOcr/lib/AipBCEUtil.php
Normal file
@@ -0,0 +1,345 @@
|
||||
<?php
|
||||
/*
|
||||
* Copyright (c) 2017 Baidu.com, Inc. All Rights Reserved
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of
|
||||
* the License at
|
||||
*
|
||||
* Http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations under
|
||||
* the License.
|
||||
*/
|
||||
namespace BaiduOcr\lib;
|
||||
/**
|
||||
* BCE Util
|
||||
*/
|
||||
class AipHttpUtil
|
||||
{
|
||||
// 根据RFC 3986,除了:
|
||||
// 1.大小写英文字符
|
||||
// 2.阿拉伯数字
|
||||
// 3.点'.'、波浪线'~'、减号'-'以及下划线'_'
|
||||
// 以外都要编码
|
||||
public static $PERCENT_ENCODED_STRINGS;
|
||||
|
||||
//填充编码数组
|
||||
public static function __init()
|
||||
{
|
||||
AipHttpUtil::$PERCENT_ENCODED_STRINGS = array();
|
||||
for ($i = 0; $i < 256; ++$i) {
|
||||
AipHttpUtil::$PERCENT_ENCODED_STRINGS[$i] = sprintf("%%%02X", $i);
|
||||
}
|
||||
|
||||
//a-z不编码
|
||||
foreach (range('a', 'z') as $ch) {
|
||||
AipHttpUtil::$PERCENT_ENCODED_STRINGS[ord($ch)] = $ch;
|
||||
}
|
||||
|
||||
//A-Z不编码
|
||||
foreach (range('A', 'Z') as $ch) {
|
||||
AipHttpUtil::$PERCENT_ENCODED_STRINGS[ord($ch)] = $ch;
|
||||
}
|
||||
|
||||
//0-9不编码
|
||||
foreach (range('0', '9') as $ch) {
|
||||
AipHttpUtil::$PERCENT_ENCODED_STRINGS[ord($ch)] = $ch;
|
||||
}
|
||||
|
||||
//以下4个字符不编码
|
||||
AipHttpUtil::$PERCENT_ENCODED_STRINGS[ord('-')] = '-';
|
||||
AipHttpUtil::$PERCENT_ENCODED_STRINGS[ord('.')] = '.';
|
||||
AipHttpUtil::$PERCENT_ENCODED_STRINGS[ord('_')] = '_';
|
||||
AipHttpUtil::$PERCENT_ENCODED_STRINGS[ord('~')] = '~';
|
||||
}
|
||||
|
||||
/**
|
||||
* 在uri编码中不能对'/'编码
|
||||
* @param string $path
|
||||
* @return string
|
||||
*/
|
||||
public static function urlEncodeExceptSlash($path)
|
||||
{
|
||||
return str_replace("%2F", "/", AipHttpUtil::urlEncode($path));
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用编码数组编码
|
||||
* @param string $path
|
||||
* @return string
|
||||
*/
|
||||
public static function urlEncode($value)
|
||||
{
|
||||
$result = '';
|
||||
for ($i = 0; $i < strlen($value); ++$i) {
|
||||
$result .= AipHttpUtil::$PERCENT_ENCODED_STRINGS[ord($value[$i])];
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成标准化QueryString
|
||||
* @param array $parameters
|
||||
* @return array
|
||||
*/
|
||||
public static function getCanonicalQueryString(array $parameters)
|
||||
{
|
||||
//没有参数,直接返回空串
|
||||
if (count($parameters) == 0) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$parameterStrings = array();
|
||||
foreach ($parameters as $k => $v) {
|
||||
//跳过Authorization字段
|
||||
if (strcasecmp('Authorization', $k) == 0) {
|
||||
continue;
|
||||
}
|
||||
if (!isset($k)) {
|
||||
throw new \InvalidArgumentException(
|
||||
"parameter key should not be null"
|
||||
);
|
||||
}
|
||||
if (isset($v)) {
|
||||
//对于有值的,编码后放在=号两边
|
||||
$parameterStrings[] = AipHttpUtil::urlEncode($k)
|
||||
. '=' . AipHttpUtil::urlEncode((string) $v);
|
||||
} else {
|
||||
//对于没有值的,只将key编码后放在=号的左边,右边留空
|
||||
$parameterStrings[] = AipHttpUtil::urlEncode($k) . '=';
|
||||
}
|
||||
}
|
||||
//按照字典序排序
|
||||
sort($parameterStrings);
|
||||
|
||||
//使用'&'符号连接它们
|
||||
return implode('&', $parameterStrings);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成标准化uri
|
||||
* @param string $path
|
||||
* @return string
|
||||
*/
|
||||
public static function getCanonicalURIPath($path)
|
||||
{
|
||||
//空路径设置为'/'
|
||||
if (empty($path)) {
|
||||
return '/';
|
||||
} else {
|
||||
//所有的uri必须以'/'开头
|
||||
if ($path[0] == '/') {
|
||||
return AipHttpUtil::urlEncodeExceptSlash($path);
|
||||
} else {
|
||||
return '/' . AipHttpUtil::urlEncodeExceptSlash($path);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成标准化http请求头串
|
||||
* @param array $headers
|
||||
* @return array
|
||||
*/
|
||||
public static function getCanonicalHeaders($headers)
|
||||
{
|
||||
//如果没有headers,则返回空串
|
||||
if (count($headers) == 0) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$headerStrings = array();
|
||||
foreach ($headers as $k => $v) {
|
||||
//跳过key为null的
|
||||
if ($k === null) {
|
||||
continue;
|
||||
}
|
||||
//如果value为null,则赋值为空串
|
||||
if ($v === null) {
|
||||
$v = '';
|
||||
}
|
||||
//trim后再encode,之后使用':'号连接起来
|
||||
$headerStrings[] = AipHttpUtil::urlEncode(strtolower(trim($k))) . ':' . AipHttpUtil::urlEncode(trim($v));
|
||||
}
|
||||
//字典序排序
|
||||
sort($headerStrings);
|
||||
|
||||
//用'\n'把它们连接起来
|
||||
return implode("\n", $headerStrings);
|
||||
}
|
||||
}
|
||||
AipHttpUtil::__init();
|
||||
|
||||
|
||||
class AipSignOption
|
||||
{
|
||||
const EXPIRATION_IN_SECONDS = 'expirationInSeconds';
|
||||
|
||||
const HEADERS_TO_SIGN = 'headersToSign';
|
||||
|
||||
const TIMESTAMP = 'timestamp';
|
||||
|
||||
const DEFAULT_EXPIRATION_IN_SECONDS = 1800;
|
||||
|
||||
const MIN_EXPIRATION_IN_SECONDS = 300;
|
||||
|
||||
const MAX_EXPIRATION_IN_SECONDS = 129600;
|
||||
}
|
||||
|
||||
|
||||
class AipSampleSigner
|
||||
{
|
||||
|
||||
const BCE_AUTH_VERSION = "bce-auth-v1";
|
||||
const BCE_PREFIX = 'x-bce-';
|
||||
|
||||
//不指定headersToSign情况下,默认签名http头,包括:
|
||||
// 1.host
|
||||
// 2.content-length
|
||||
// 3.content-type
|
||||
// 4.content-md5
|
||||
public static $defaultHeadersToSign;
|
||||
|
||||
public static function __init()
|
||||
{
|
||||
AipSampleSigner::$defaultHeadersToSign = array(
|
||||
"host",
|
||||
"content-length",
|
||||
"content-type",
|
||||
"content-md5",
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 签名
|
||||
* @param array $credentials
|
||||
* @param string $httpMethod
|
||||
* @param string $path
|
||||
* @param array $headers
|
||||
* @param string $params
|
||||
* @param array $options
|
||||
* @return string
|
||||
*/
|
||||
public static function sign(
|
||||
array $credentials,
|
||||
$httpMethod,
|
||||
$path,
|
||||
$headers,
|
||||
$params,
|
||||
$options = array()
|
||||
) {
|
||||
//设定签名有效时间
|
||||
if (!isset($options[AipSignOption::EXPIRATION_IN_SECONDS])) {
|
||||
//默认值1800秒
|
||||
$expirationInSeconds = AipSignOption::DEFAULT_EXPIRATION_IN_SECONDS;
|
||||
} else {
|
||||
$expirationInSeconds = $options[AipSignOption::EXPIRATION_IN_SECONDS];
|
||||
}
|
||||
|
||||
//解析ak sk
|
||||
$accessKeyId = $credentials['ak'];
|
||||
$secretAccessKey = $credentials['sk'];
|
||||
|
||||
//设定时间戳,注意:如果自行指定时间戳需要为UTC时间
|
||||
if (!isset($options[AipSignOption::TIMESTAMP])) {
|
||||
//默认值当前时间
|
||||
$timestamp = gmdate('Y-m-d\TH:i:s\Z');
|
||||
} else {
|
||||
$timestamp = $options[AipSignOption::TIMESTAMP];
|
||||
}
|
||||
|
||||
//生成authString
|
||||
$authString = AipSampleSigner::BCE_AUTH_VERSION . '/' . $accessKeyId . '/'
|
||||
. $timestamp . '/' . $expirationInSeconds;
|
||||
|
||||
//使用sk和authString生成signKey
|
||||
$signingKey = hash_hmac('sha256', $authString, $secretAccessKey);
|
||||
|
||||
//生成标准化URI
|
||||
$canonicalURI = AipHttpUtil::getCanonicalURIPath($path);
|
||||
|
||||
//生成标准化QueryString
|
||||
$canonicalQueryString = AipHttpUtil::getCanonicalQueryString($params);
|
||||
|
||||
//填充headersToSign,也就是指明哪些header参与签名
|
||||
$headersToSign = null;
|
||||
if (isset($options[AipSignOption::HEADERS_TO_SIGN])) {
|
||||
$headersToSign = $options[AipSignOption::HEADERS_TO_SIGN];
|
||||
}
|
||||
|
||||
//生成标准化header
|
||||
$canonicalHeader = AipHttpUtil::getCanonicalHeaders(
|
||||
AipSampleSigner::getHeadersToSign($headers, $headersToSign)
|
||||
);
|
||||
|
||||
//整理headersToSign,以';'号连接
|
||||
$signedHeaders = '';
|
||||
if ($headersToSign !== null) {
|
||||
$signedHeaders = strtolower(
|
||||
trim(implode(";", $headersToSign))
|
||||
);
|
||||
}
|
||||
|
||||
//组成标准请求串
|
||||
$canonicalRequest = "$httpMethod\n$canonicalURI\n"
|
||||
. "$canonicalQueryString\n$canonicalHeader";
|
||||
|
||||
//使用signKey和标准请求串完成签名
|
||||
$signature = hash_hmac('sha256', $canonicalRequest, $signingKey);
|
||||
|
||||
//组成最终签名串
|
||||
$authorizationHeader = "$authString/$signedHeaders/$signature";
|
||||
|
||||
return $authorizationHeader;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据headsToSign过滤应该参与签名的header
|
||||
* @param array $headers
|
||||
* @param array $headersToSign
|
||||
* @return array
|
||||
*/
|
||||
public static function getHeadersToSign($headers, $headersToSign)
|
||||
{
|
||||
|
||||
$arr = array();
|
||||
foreach ($headersToSign as $value) {
|
||||
$arr[] = strtolower(trim($value));
|
||||
}
|
||||
|
||||
//value被trim后为空串的header不参与签名
|
||||
$result = array();
|
||||
foreach ($headers as $key => $value) {
|
||||
if (trim($value) !== '') {
|
||||
$key = strtolower(trim($key));
|
||||
if (in_array($key, $arr)) {
|
||||
$result[$key] = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//返回需要参与签名的header
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查header是不是默认参加签名的:
|
||||
* 1.是host、content-type、content-md5、content-length之一
|
||||
* 2.以x-bce开头
|
||||
* @param array $header
|
||||
* @return boolean
|
||||
*/
|
||||
public static function isDefaultHeaderToSign($header)
|
||||
{
|
||||
$header = strtolower(trim($header));
|
||||
if (in_array($header, AipSampleSigner::$defaultHeadersToSign)) {
|
||||
return true;
|
||||
}
|
||||
return substr_compare($header, AipSampleSigner::BCE_PREFIX, 0, strlen(AipSampleSigner::BCE_PREFIX)) == 0;
|
||||
}
|
||||
}
|
||||
AipSampleSigner::__init();
|
||||
398
金壶/ydyd_service/extend/BaiduOcr/lib/AipBase.php
Normal file
398
金壶/ydyd_service/extend/BaiduOcr/lib/AipBase.php
Normal file
@@ -0,0 +1,398 @@
|
||||
<?php
|
||||
/*
|
||||
* Copyright (c) 2017 Baidu.com, Inc. All Rights Reserved
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of
|
||||
* the License at
|
||||
*
|
||||
* Http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations under
|
||||
* the License.
|
||||
*/
|
||||
namespace BaiduOcr\lib;
|
||||
//require_once 'AipHttpClient.php';
|
||||
//require_once 'AipBCEUtil.php';
|
||||
|
||||
|
||||
use BaiduOcr\lib\AipHttpClient as AipHttpClient;
|
||||
use BaiduOcr\lib\AipBCEUtil as AipBCEUtil;
|
||||
|
||||
/**
|
||||
* Aip Base 基类
|
||||
*/
|
||||
class AipBase {
|
||||
|
||||
/**
|
||||
* 获取access token url
|
||||
* @var string
|
||||
*/
|
||||
protected $accessTokenUrl = 'https://aip.baidubce.com/oauth/2.0/token';
|
||||
|
||||
/**
|
||||
* 反馈接口
|
||||
* @var string
|
||||
*/
|
||||
protected $reportUrl = 'https://aip.baidubce.com/rpc/2.0/feedback/v1/report';
|
||||
|
||||
/**
|
||||
* appId
|
||||
* @var string
|
||||
*/
|
||||
protected $appId = '';
|
||||
|
||||
/**
|
||||
* apiKey
|
||||
* @var string
|
||||
*/
|
||||
protected $apiKey = '';
|
||||
|
||||
/**
|
||||
* secretKey
|
||||
* @var string
|
||||
*/
|
||||
protected $secretKey = '';
|
||||
|
||||
/**
|
||||
* 权限
|
||||
* @var array
|
||||
*/
|
||||
protected $scope = 'brain_all_scope';
|
||||
|
||||
/**
|
||||
* @param string $appId
|
||||
* @param string $apiKey
|
||||
* @param string $secretKey
|
||||
*/
|
||||
public function __construct($appId, $apiKey, $secretKey){
|
||||
$this->appId = trim($appId);
|
||||
$this->apiKey = trim($apiKey);
|
||||
$this->secretKey = trim($secretKey);
|
||||
$this->isCloudUser = null;
|
||||
$this->client = new AipHttpClient();
|
||||
$this->version = '2_2_15';
|
||||
$this->proxies = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看版本
|
||||
* @return string
|
||||
*
|
||||
*/
|
||||
public function getVersion(){
|
||||
return $this->version;
|
||||
}
|
||||
|
||||
/**
|
||||
* 连接超时
|
||||
* @param int $ms 毫秒
|
||||
*/
|
||||
public function setConnectionTimeoutInMillis($ms){
|
||||
$this->client->setConnectionTimeoutInMillis($ms);
|
||||
}
|
||||
|
||||
/**
|
||||
* 响应超时
|
||||
* @param int $ms 毫秒
|
||||
*/
|
||||
public function setSocketTimeoutInMillis($ms){
|
||||
$this->client->setSocketTimeoutInMillis($ms);
|
||||
}
|
||||
|
||||
/**
|
||||
* 代理
|
||||
* @param array $proxy
|
||||
* @return string
|
||||
*
|
||||
*/
|
||||
public function setProxies($proxies){
|
||||
$this->client->setConf($proxies);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理请求参数
|
||||
* @param string $url
|
||||
* @param array $params
|
||||
* @param array $data
|
||||
* @param array $headers
|
||||
*/
|
||||
protected function proccessRequest($url, &$params, &$data, $headers){
|
||||
$params['aipSdk'] = 'php';
|
||||
$params['aipSdkVersion'] = $this->version;
|
||||
}
|
||||
|
||||
/**
|
||||
* Api 请求
|
||||
* @param string $url
|
||||
* @param mixed $data
|
||||
* @return mixed
|
||||
*/
|
||||
protected function request($url, $data, $headers=array()){
|
||||
try{
|
||||
$result = $this->validate($url, $data);
|
||||
if($result !== true){
|
||||
return $result;
|
||||
}
|
||||
|
||||
$params = array();
|
||||
$authObj = $this->auth();
|
||||
|
||||
if($this->isCloudUser === false){
|
||||
$params['access_token'] = $authObj['access_token'];
|
||||
}
|
||||
|
||||
// 特殊处理
|
||||
$this->proccessRequest($url, $params, $data, $headers);
|
||||
|
||||
$headers = $this->getAuthHeaders('POST', $url, $params, $headers);
|
||||
$response = $this->client->post($url, $data, $params, $headers);
|
||||
|
||||
$obj = $this->proccessResult($response['content']);
|
||||
|
||||
if(!$this->isCloudUser && isset($obj['error_code']) && $obj['error_code'] == 110){
|
||||
$authObj = $this->auth(true);
|
||||
$params['access_token'] = $authObj['access_token'];
|
||||
$response = $this->client->post($url, $data, $params, $headers);
|
||||
$obj = $this->proccessResult($response['content']);
|
||||
}
|
||||
|
||||
if(empty($obj) || !isset($obj['error_code'])){
|
||||
$this->writeAuthObj($authObj);
|
||||
}
|
||||
}catch(Exception $e){
|
||||
return array(
|
||||
'error_code' => 'SDK108',
|
||||
'error_msg' => 'connection or read data timeout',
|
||||
);
|
||||
}
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* Api 多个并发请求
|
||||
* @param string $url
|
||||
* @param mixed $data
|
||||
* @return mixed
|
||||
*/
|
||||
protected function multi_request($url, $data){
|
||||
try{
|
||||
$params = array();
|
||||
$authObj = $this->auth();
|
||||
$headers = $this->getAuthHeaders('POST', $url);
|
||||
|
||||
if($this->isCloudUser === false){
|
||||
$params['access_token'] = $authObj['access_token'];
|
||||
}
|
||||
|
||||
$responses = $this->client->multi_post($url, $data, $params, $headers);
|
||||
|
||||
$is_success = false;
|
||||
foreach($responses as $response){
|
||||
$obj = $this->proccessResult($response['content']);
|
||||
|
||||
if(empty($obj) || !isset($obj['error_code'])){
|
||||
$is_success = true;
|
||||
}
|
||||
|
||||
if(!$this->isCloudUser && isset($obj['error_code']) && $obj['error_code'] == 110){
|
||||
$authObj = $this->auth(true);
|
||||
$params['access_token'] = $authObj['access_token'];
|
||||
$responses = $this->client->post($url, $data, $params, $headers);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if($is_success){
|
||||
$this->writeAuthObj($authObj);
|
||||
}
|
||||
|
||||
$objs = array();
|
||||
foreach($responses as $response){
|
||||
$objs[] = $this->proccessResult($response['content']);
|
||||
}
|
||||
|
||||
}catch(Exception $e){
|
||||
return array(
|
||||
'error_code' => 'SDK108',
|
||||
'error_msg' => 'connection or read data timeout',
|
||||
);
|
||||
}
|
||||
|
||||
return $objs;
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式检查
|
||||
* @param string $url
|
||||
* @param array $data
|
||||
* @return mix
|
||||
*/
|
||||
protected function validate($url, &$data){
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化结果
|
||||
* @param $content string
|
||||
* @return mixed
|
||||
*/
|
||||
protected function proccessResult($content){
|
||||
return json_decode($content, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回 access token 路径
|
||||
* @return string
|
||||
*/
|
||||
private function getAuthFilePath(){
|
||||
return dirname(__FILE__) . DIRECTORY_SEPARATOR . md5($this->apiKey);
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入本地文件
|
||||
* @param array $obj
|
||||
* @return void
|
||||
*/
|
||||
private function writeAuthObj($obj){
|
||||
if($obj === null || (isset($obj['is_read']) && $obj['is_read'] === true)){
|
||||
return;
|
||||
}
|
||||
|
||||
$obj['time'] = time();
|
||||
$obj['is_cloud_user'] = $this->isCloudUser;
|
||||
@file_put_contents($this->getAuthFilePath(), json_encode($obj));
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取本地缓存
|
||||
* @return array
|
||||
*/
|
||||
private function readAuthObj(){
|
||||
$content = @file_get_contents($this->getAuthFilePath());
|
||||
if($content !== false){
|
||||
$obj = json_decode($content, true);
|
||||
$this->isCloudUser = $obj['is_cloud_user'];
|
||||
$obj['is_read'] = true;
|
||||
if($this->isCloudUser || $obj['time'] + $obj['expires_in'] - 30 > time()){
|
||||
return $obj;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 认证
|
||||
* @param bool $refresh 是否刷新
|
||||
* @return array
|
||||
*/
|
||||
private function auth($refresh=false){
|
||||
|
||||
//非过期刷新
|
||||
if(!$refresh){
|
||||
$obj = $this->readAuthObj();
|
||||
if(!empty($obj)){
|
||||
return $obj;
|
||||
}
|
||||
}
|
||||
|
||||
$response = $this->client->get($this->accessTokenUrl, array(
|
||||
'grant_type' => 'client_credentials',
|
||||
'client_id' => $this->apiKey,
|
||||
'client_secret' => $this->secretKey,
|
||||
));
|
||||
|
||||
$obj = json_decode($response['content'], true);
|
||||
|
||||
$this->isCloudUser = !$this->isPermission($obj);
|
||||
return $obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断认证是否有权限
|
||||
* @param array $authObj
|
||||
* @return boolean
|
||||
*/
|
||||
protected function isPermission($authObj)
|
||||
{
|
||||
if(empty($authObj) || !isset($authObj['scope'])){
|
||||
return false;
|
||||
}
|
||||
|
||||
$scopes = explode(' ', $authObj['scope']);
|
||||
|
||||
return in_array($this->scope, $scopes);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $method HTTP method
|
||||
* @param string $url
|
||||
* @param array $param 参数
|
||||
* @return array
|
||||
*/
|
||||
private function getAuthHeaders($method, $url, $params=array(), $headers=array()){
|
||||
|
||||
//不是云的老用户则不用在header中签名 认证
|
||||
if($this->isCloudUser === false){
|
||||
return $headers;
|
||||
}
|
||||
|
||||
$obj = parse_url($url);
|
||||
if(!empty($obj['query'])){
|
||||
foreach(explode('&', $obj['query']) as $kv){
|
||||
if(!empty($kv)){
|
||||
list($k, $v) = explode('=', $kv, 2);
|
||||
$params[$k] = $v;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//UTC 时间戳
|
||||
$timestamp = gmdate('Y-m-d\TH:i:s\Z');
|
||||
$headers['Host'] = isset($obj['port']) ? sprintf('%s:%s', $obj['host'], $obj['port']) : $obj['host'];
|
||||
$headers['x-bce-date'] = $timestamp;
|
||||
|
||||
//签名
|
||||
$headers['authorization'] = AipSampleSigner::sign(array(
|
||||
'ak' => $this->apiKey,
|
||||
'sk' => $this->secretKey,
|
||||
), $method, $obj['path'], $headers, $params, array(
|
||||
'timestamp' => $timestamp,
|
||||
'headersToSign' => array_keys($headers),
|
||||
));
|
||||
|
||||
return $headers;
|
||||
}
|
||||
|
||||
/**
|
||||
* 反馈
|
||||
*
|
||||
* @param array $feedbacks
|
||||
* @return array
|
||||
*/
|
||||
public function report($feedback){
|
||||
|
||||
$data = array();
|
||||
|
||||
$data['feedback'] = $feedback;
|
||||
|
||||
return $this->request($this->reportUrl, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通用接口
|
||||
* @param string $url
|
||||
* @param array $data
|
||||
* @param array header
|
||||
* @return array
|
||||
*/
|
||||
public function post($url, $data, $headers=array()){
|
||||
return $this->request($url, $data, $headers);
|
||||
}
|
||||
|
||||
}
|
||||
215
金壶/ydyd_service/extend/BaiduOcr/lib/AipHttpClient.php
Normal file
215
金壶/ydyd_service/extend/BaiduOcr/lib/AipHttpClient.php
Normal file
@@ -0,0 +1,215 @@
|
||||
<?php
|
||||
/*
|
||||
* Copyright (c) 2017 Baidu.com, Inc. All Rights Reserved
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
|
||||
* use this file except in compliance with the License. You may obtain a copy of
|
||||
* the License at
|
||||
*
|
||||
* Http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations under
|
||||
* the License.
|
||||
*/
|
||||
namespace BaiduOcr\lib;
|
||||
|
||||
/**
|
||||
* Http Client
|
||||
*/
|
||||
class AipHttpClient{
|
||||
|
||||
/**
|
||||
* HttpClient
|
||||
* @param array $headers HTTP header
|
||||
*/
|
||||
public function __construct($headers=array()){
|
||||
$this->headers = $this->buildHeaders($headers);
|
||||
$this->connectTimeout = 60000;
|
||||
$this->socketTimeout = 60000;
|
||||
$this->conf = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* 连接超时
|
||||
* @param int $ms 毫秒
|
||||
*/
|
||||
public function setConnectionTimeoutInMillis($ms){
|
||||
$this->connectTimeout = $ms;
|
||||
}
|
||||
|
||||
/**
|
||||
* 响应超时
|
||||
* @param int $ms 毫秒
|
||||
*/
|
||||
public function setSocketTimeoutInMillis($ms){
|
||||
$this->socketTimeout = $ms;
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置
|
||||
* @param array $conf
|
||||
*/
|
||||
public function setConf($conf){
|
||||
$this->conf = $conf;
|
||||
}
|
||||
|
||||
/**
|
||||
* 请求预处理
|
||||
* @param resource $ch
|
||||
*/
|
||||
public function prepare($ch){
|
||||
foreach($this->conf as $key => $value){
|
||||
curl_setopt($ch, $key, $value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $url
|
||||
* @param array $data HTTP POST BODY
|
||||
* @param array $param HTTP URL
|
||||
* @param array $headers HTTP header
|
||||
* @return array
|
||||
*/
|
||||
public function post($url, $data=array(), $params=array(), $headers=array()){
|
||||
$url = $this->buildUrl($url, $params);
|
||||
$headers = array_merge($this->headers, $this->buildHeaders($headers));
|
||||
|
||||
$ch = curl_init();
|
||||
$this->prepare($ch);
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_POST, 1);
|
||||
curl_setopt($ch, CURLOPT_HEADER, false);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, is_array($data) ? http_build_query($data) : $data);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT_MS, $this->socketTimeout);
|
||||
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, $this->connectTimeout);
|
||||
$content = curl_exec($ch);
|
||||
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
|
||||
if($code === 0){
|
||||
throw new Exception(curl_error($ch));
|
||||
}
|
||||
|
||||
curl_close($ch);
|
||||
return array(
|
||||
'code' => $code,
|
||||
'content' => $content,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $url
|
||||
* @param array $datas HTTP POST BODY
|
||||
* @param array $param HTTP URL
|
||||
* @param array $headers HTTP header
|
||||
* @return array
|
||||
*/
|
||||
public function multi_post($url, $datas=array(), $params=array(), $headers=array()){
|
||||
$url = $this->buildUrl($url, $params);
|
||||
$headers = array_merge($this->headers, $this->buildHeaders($headers));
|
||||
|
||||
$chs = array();
|
||||
$result = array();
|
||||
$mh = curl_multi_init();
|
||||
foreach($datas as $data){
|
||||
$ch = curl_init();
|
||||
$chs[] = $ch;
|
||||
$this->prepare($ch);
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_POST, 1);
|
||||
curl_setopt($ch, CURLOPT_HEADER, false);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, is_array($data) ? http_build_query($data) : $data);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT_MS, $this->socketTimeout);
|
||||
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, $this->connectTimeout);
|
||||
curl_multi_add_handle($mh, $ch);
|
||||
}
|
||||
|
||||
$running = null;
|
||||
do{
|
||||
curl_multi_exec($mh, $running);
|
||||
usleep(100);
|
||||
}while($running);
|
||||
|
||||
foreach($chs as $ch){
|
||||
$content = curl_multi_getcontent($ch);
|
||||
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
$result[] = array(
|
||||
'code' => $code,
|
||||
'content' => $content,
|
||||
);
|
||||
curl_multi_remove_handle($mh, $ch);
|
||||
}
|
||||
curl_multi_close($mh);
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $url
|
||||
* @param array $param HTTP URL
|
||||
* @param array $headers HTTP header
|
||||
* @return array
|
||||
*/
|
||||
public function get($url, $params=array(), $headers=array()){
|
||||
$url = $this->buildUrl($url, $params);
|
||||
$headers = array_merge($this->headers, $this->buildHeaders($headers));
|
||||
|
||||
$ch = curl_init();
|
||||
$this->prepare($ch);
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
curl_setopt($ch, CURLOPT_HEADER, false);
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT_MS, $this->socketTimeout);
|
||||
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, $this->connectTimeout);
|
||||
$content = curl_exec($ch);
|
||||
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
|
||||
if($code === 0){
|
||||
throw new Exception(curl_error($ch));
|
||||
}
|
||||
|
||||
curl_close($ch);
|
||||
return array(
|
||||
'code' => $code,
|
||||
'content' => $content,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造 header
|
||||
* @param array $headers
|
||||
* @return array
|
||||
*/
|
||||
private function buildHeaders($headers){
|
||||
$result = array();
|
||||
foreach($headers as $k => $v){
|
||||
$result[] = sprintf('%s:%s', $k, $v);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param string $url
|
||||
* @param array $params 参数
|
||||
* @return string
|
||||
*/
|
||||
private function buildUrl($url, $params){
|
||||
if(!empty($params)){
|
||||
$str = http_build_query($params);
|
||||
return $url . (strpos($url, '?') === false ? '?' : '&') . $str;
|
||||
}else{
|
||||
return $url;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
{"refresh_token":"25.d3afd58367f9b83c9c1050351a39949c.315360000.1885190493.282335-17393063","expires_in":2592000,"session_key":"9mzdWBXHl3rzZnYwDbIXWIOP13psR1DcWcRYjn9l90\/wnKw0yRpXvLeBgOCseBb0R1Z8CQpSnLPpCvtY1aLNGb9wgQoecQ==","access_token":"24.cea0ed80a1cd81807dbfe827e6de264f.2592000.1572422493.282335-17393063","scope":"public vis-ocr_ocr brain_ocr_scope brain_ocr_general brain_ocr_general_basic vis-ocr_business_license brain_ocr_webimage brain_all_scope brain_ocr_idcard brain_ocr_driving_license brain_ocr_vehicle_license vis-ocr_plate_number brain_solution brain_ocr_plate_number brain_ocr_accurate brain_ocr_accurate_basic brain_ocr_receipt brain_ocr_business_license brain_solution_iocr brain_qrcode brain_ocr_handwriting brain_antispam_spam brain_ocr_passport brain_ocr_vat_invoice brain_numbers brain_ocr_business_card brain_nlp_ecnet brain_ocr_train_ticket brain_ocr_taxi_receipt vis-ocr_household_register vis-ocr_vis-classify_birth_certificate vis-ocr_\u53f0\u6e7e\u901a\u884c\u8bc1 vis-ocr_\u6e2f\u6fb3\u901a\u884c\u8bc1 vis-ocr_\u673a\u52a8\u8f66\u68c0\u9a8c\u5408\u683c\u8bc1\u8bc6\u522b vis-ocr_\u8f66\u8f86vin\u7801\u8bc6\u522b solution_face vis-ocr_\u5b9a\u989d\u53d1\u7968\u8bc6\u522b vis-ocr_\u4fdd\u5355\u8bc6\u522b brain_ocr_vin brain_ocr_quota_invoice brain_ocr_birth_certificate brain_ocr_household_register brain_ocr_HK_Macau_pass brain_ocr_taiwan_pass brain_ocr_vehicle_certificate brain_ocr_insurance_doc wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test\u6743\u9650 vis-classify_flower lpq_\u5f00\u653e cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_\u5f00\u653eScope","session_secret":"30a863980c0cab64b861f52619711d5d","time":1569830494,"is_cloud_user":false}
|
||||
@@ -0,0 +1 @@
|
||||
{"refresh_token":"25.8a38b4f72b23bfd9873904847c09b490.315360000.1900484860.282335-17614601","expires_in":2592000,"session_key":"9mzdCuD\/nrbL3I0WLtbqw3oaxKRdlkjNID355V4Pavpe\/8ILowiVfMTHMqLnRQeC8ENKjnaFUDKbaE8QFmLYBWd3xFv+1w==","access_token":"24.1d3dd93c6453ea6f71009fdf8421170e.2592000.1587716860.282335-17614601","scope":"vis-ocr_\u884c\u7a0b\u5355\u8bc6\u522b brain_ocr_air_ticket public vis-ocr_ocr brain_ocr_scope brain_ocr_general brain_ocr_general_basic vis-ocr_business_license brain_ocr_webimage brain_all_scope brain_ocr_idcard brain_ocr_driving_license brain_ocr_vehicle_license vis-ocr_plate_number brain_solution brain_ocr_plate_number brain_ocr_accurate brain_ocr_accurate_basic brain_ocr_receipt brain_ocr_business_license brain_solution_iocr brain_qrcode brain_ocr_handwriting brain_ocr_passport brain_ocr_vat_invoice brain_numbers brain_ocr_business_card brain_ocr_train_ticket brain_ocr_taxi_receipt vis-ocr_household_register vis-ocr_vis-classify_birth_certificate vis-ocr_\u53f0\u6e7e\u901a\u884c\u8bc1 vis-ocr_\u6e2f\u6fb3\u901a\u884c\u8bc1 vis-ocr_\u673a\u52a8\u8f66\u68c0\u9a8c\u5408\u683c\u8bc1\u8bc6\u522b vis-ocr_\u8f66\u8f86vin\u7801\u8bc6\u522b vis-ocr_\u5b9a\u989d\u53d1\u7968\u8bc6\u522b vis-ocr_\u4fdd\u5355\u8bc6\u522b brain_ocr_vin brain_ocr_quota_invoice brain_ocr_birth_certificate brain_ocr_household_register brain_ocr_HK_Macau_pass brain_ocr_taiwan_pass brain_ocr_vehicle_certificate brain_ocr_insurance_doc wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test\u6743\u9650 vis-classify_flower lpq_\u5f00\u653e cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi qatest_scope1 fake_face_detect_\u5f00\u653eScope vis-ocr_\u865a\u62df\u4eba\u7269\u52a9\u7406 idl-video_\u865a\u62df\u4eba\u7269\u52a9\u7406","session_secret":"4df7ce78d05af9d18d308e000fdf7c03","time":1585124860,"is_cloud_user":false}
|
||||
Reference in New Issue
Block a user