用户的营业执照信息和身份证信息需要使用OCR接口读出来,减少用户输入,增加用户体验。
准备工作
安装 PHP SDK 前,先获取安全凭证。在第一次使用云API之前,用户首先需要在腾讯云控制台上申请安全凭证,安全凭证包括 SecretID 和 SecretKey, SecretID 是用于标识 API 调用者的身份,SecretKey是用于加密签名字符串和服务器端验证签名字符串的密钥。SecretKey 必须严格保管,避免泄露。
通过composer安装
-
安装Composer: windows环境请访问Composer官网下载安装包安装。
unix环境在命令行中执行以下命令安装。
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
-
建议中国大陆地区的用户设置腾讯云镜像源:
composer config -g repos.packagist composer https://mirrors.tencent.com/composer/
-
执行命令
composer require tencentcloud/tencentcloud-sdk-php
添加依赖。 -
在代码中添加以下引用代码。注意:如下仅为示例,composer 会在项目根目录下生成 vendor 目录,
/path/to/
为项目根目录的实际绝对路径,如果是在当前目录执行,可以省略绝对路径。require '/path/to/vendor/autoload.php';
注:这一步在laravel里因为有自动加载,所以可以省略‘
5.composer 内存不够问题
php -d memory_limit=512M /usr/local/bin/composer install -vvv
用这个命令可以强制使用置顶内存执行。我解决的办法是在php.ini中把memory_limit设置成了-1,不限制内存。因为腾讯云有个大包会执行unzip操作,所以docker等容器,需要磁盘读写性能好,不然很慢。比如我的windows docker。
获取Demo
非常奇葩的一点,在vendor的扩展中,你看到的example很少,而且看起来已经停止更新。
这里建议使用腾讯云的自动生成代码页面(需要微信扫码登录,不需要实名认证)https://console.cloud.tencent.com/api/explorer?Product=faceid&Version=2018-03-01&Action=IdCardVerification
例如这个链接,就是身份证代码生成页面。在左侧可以选自己的SDK。
![](https://img.haomeiwen.com/i14988112/437a9c6f3dda1492.png)
打开后如图,可以搜索自己要用的SDK,右侧会自动生成代码。
简单封装
当前使用的是面向过程的方法,不符合我们的使用习惯,因为很多ocr可以公用一个client对象,所以我们对client进行简单的单例模式封装。
...省略命名空间,按需调用
use TencentCloud\Common\Credential;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Ocr\V20181119\Models\IDCardOCRRequest;
use TencentCloud\Ocr\V20181119\OcrClient;
use TencentCloud\Ocr\V20181119\Models\BizLicenseOCRRequest;
class Tencent
{
public static $_client;
/**
* @return \Qcloud\Cos\Client
*/
public static function getClient()
{
if (self::$_client == null) {
$cred = new Credential(config('qcloud.secret.secretId'), config('qcloud.secret.secretKey'));
$httpProfile = new HttpProfile();
//这里的地址表示就近接入
$httpProfile->setEndpoint("ocr.tencentcloudapi.com");
$clientProfile = new ClientProfile();
$clientProfile->setHttpProfile($httpProfile);
self::$_client = new OcrClient($cred, config('qcloud.secret.region'), $clientProfile);
}
return self::$_client;
}
public static function getLicenceInfo($url)
{
try {
$req = new BizLicenseOCRRequest();
$params = json_encode([
'ImageUrl' => $url
]);
$req->fromJsonString($params);
$resp = self::getClient()->BizLicenseOCR($req);
return $resp->toJsonString();
} catch (TencentCloudSDKException $e) {
return $e->getMessage();
}
}
//FRONT正面,BACK背面
public static function getIDCardInfo($url,$side = 'FRONT')
{
try {
$req = new IDCardOCRRequest();
$params = json_encode([
'ImageUrl' => $url,
'CardSide' => $side
]);
$req->fromJsonString($params);
$resp = self::getClient()->IDCardOCR($req);
return $resp->toJsonString();
} catch (TencentCloudSDKException $e) {
return $e->getMessage();
}
}
}
我这里没有用三私一公的方式,懒~~~
简单调用
$jsoninfo = LicenceOCR::getIDCardInfo($url,'FRONT');
$info = json_decode($jsoninfo,true);
if(!$info){
return $this->error(502,$jsoninfo);
}
返回值为json或者是异常返回的报错,所以要加个判断。
网友评论