腾讯云给的api直接就能用。
首先,先下载腾讯OCR
composer require tencentcloud/tencentcloud-sdk-php
然后打开腾讯的线上调试地址
https://console.cloud.tencent.com/api/explorer?Product=ocr&Version=2018-11-19&Action=GeneralBasicOCR&SignVersion=
输入什么SecretId等参数之后,在右侧选择php,就会生成在php中使用的代码
<?php
require_once 'vendor/autoload.php';
use TencentCloud\Common\Credential;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Ocr\V20181119\OcrClient;
use TencentCloud\Ocr\V20181119\Models\GeneralBasicOCRRequest;
try {
$cred = new Credential("SecretId", "SecretKey");
$httpProfile = new HttpProfile();
$httpProfile->setEndpoint("ocr.tencentcloudapi.com");
$clientProfile = new ClientProfile();
$clientProfile->setHttpProfile($httpProfile);
$client = new OcrClient($cred, "ap-beijing", $clientProfile);
$req = new GeneralBasicOCRRequest();
$params = array(
"ImageUrl" => "https://other-1303057111.cos.ap-nanjing.myqcloud.com/shibie.jpg"
);
$req->fromJsonString(json_encode($params));
$resp = $client->GeneralBasicOCR($req);
print_r($resp->toJsonString());
}
catch(TencentCloudSDKException $e) {
echo $e;
}
直接把代码放在控制器相应的位置,就能直接用了。值得一说的是,最上面那个autoload.php不需要写。
如果是本地环境,可能会报一个SSL错误
解决办法:
下载cacert.pem
将cacert.pem放到PHP根目录
然后打开PHP.ini,找到curl.cainfo,删除前面的“;”
然后curl.cainfo=cacert.pem的绝对路径,就是从盘符开始的绝对路径,F:/php/xxxxx
重启php服务器,就OVER了