一、登录百度AI开放平台创建orc应用
1、选择文字识别→创建应用→做好记录后续使用下面三个数据
2、选择需要操作的文字识别→买入身份识别和银行卡识别(根据自己实际需求选,这个返回的比较全面,有人头像,所以选择这分开的,要是只需要信息,买通用就行)
二、下载sdk包
我使用的框架是php的,thinkphp6。下载包地址:https://ai.baidu.com/sdk#ocr
三、tp框架引入sdk包
在项目三方类库vendor中新建一个ocr文件夹,把下载好的sdk包放入到里面:
四、tp6相关代码使用
1、在app文件夹下,新建一个公用文件夹common 放入两个文件 Ai.php Image.php(用于base64上传)。同时在页面上引入
2、\new\app\common\Ai.php’
<?php
namespace app\common;
use think\facade\Config;
require_once '../vendor/ocr/AipOcr.php';
class Ai
{
/**
* phpmailer对象
*/
protected $client;
/**
* 构造函数
*/
public function __construct()
{
$config = Config::get('ocr');
$this->client = new \AipOcr($config['ai_app_id'],$config['ai_api_key'],$config['ai_secret_key']);
}
static private function _check_file_path_type_is_url($file_path) {
if (preg_match('/http/', $file_path)) {
return true;
}
return false;
}
/**
* 身份证识别
* @param $file_path //图片地址
* @param $front //正反
*/
public function getIdCard($file_path,$front) {
if (self::_check_file_path_type_is_url($file_path)) {
return $this->client->idcard($file_path);
} else {
$image = file_get_contents($file_path);
return $this->client->idcard($image,$front);
}
}
/**
* 银行卡识别
* @param $file_path //图片地址
*/
public function getBankCard($file_path) {
if (self::_check_file_path_type_is_url($file_path)) {
return $this->client->bankcard($file_path);
} else {
$image = file_get_contents($file_path);
return $this->client->bankcard($image);
}
}
public function request_post($url = '', $param = '') {
if (empty($url) || empty($param)) {
return false;
}
// var_dump($param);exit;
$postUrl = $url;
$curlPost = $param;
$curl = curl_init();//初始化curl
curl_setopt($curl, CURLOPT_URL,$postUrl);//抓取指定网页
curl_setopt($curl, CURLOPT_HEADER</