首先就是七牛的接口sdk:https://github.com/qiniu/php-sdk;
然后就是车辆登记 OCR_API文档:https://developer.qiniu.com/dora/api/7031/vehicle-registration-ocr;
文档内容:
接口简介
检测车辆登记证的图片,返回 OCR 识别结果。
注意
*接口超时时间为 30 秒
*资源的 Mime 文件格式仅支持图片类型:image/*,如:image/jpeg、image/png。
*请求接口为标准的HTTP/HTTPS 协议。
请求语法
POST /ocr/cz HTTP/1.1
Host: ocr-cz.qiniuapi.com
Content-Type: application/json
Authorization: QiniuToken
头部信息头部名称 必填 说明
Host 是 固定为 ocr-cz.qiniuapi.com
Content-Type 是 固定为 application/json
Authorization 是 该参数应严格按照管理凭证格式进行填充,否则会返回 401 错误码。一个合法的 Authorization 值应类似于:Qiniu QNJi_bYJlmO5LeY08FfoNj9w_r7...
请求body
字段 是否必填 类型 说明
image Y string ba
uuid N string 唯一会话 id
可以直接访问官方文档查看;
我是下载的官方sdk的压缩文件安装的,安装方法:
1、将sdk放入vender中,
2、在入口文件处引入sdk的autoload.php
//依蓝云(七牛)oss服务器
require_once __DIR__ .'/../vendor/qiniu-sdk/autoload.php';
3、编辑接口:在控制器中引入:use Qiniu\Auth;
在方法中编写: // 需要填写你的 Access Key 和 Secret Key
$accessKey = '你的ak';
$secretKey = '你的sk';
// 构建鉴权对象
$auth = new Auth($accessKey, $secretKey);
//图片地址;
$file="uploads/20200311/20201224143711.jpg";
$file_base64=imgToBase64($file);
$body=json_encode(['image'=>$file_base64]);
//$body="{'image':'".$file_base64."'}";
$url="http://ocr-cz.qiniuapi.com/ocr/cz";
$host="ocr-cz.qiniuapi.com";
$contentType="application/json";
$method='POST';
$header=$auth->authorizationV2($url,$method,$body,$contentType);
$header['Content-type']= $contentType;
$header['Host']= $host;
$head=[];
foreach($header as $k=>$v){
$head[]=$k.':'.$v;
}
$data=$this->post_json_data($url,$body,$head);
dump( json_decode($data,true));
附赠curl请求方法:[b][/b]function post_json_data($url, $data_string,$aHeader,$timeout=300) {
if(!$aHeader){
$length=strlen($data_string);
$aHeader= array('Content-type:application/json', 'Content-length:'.$length);
}
// dump($aHeader);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
//设置头文件的信息作为数据流输出
//curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
// post数据
curl_setopt($ch, CURLOPT_POST, true);
// post的变量
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
$file_contents = curl_exec($ch);
curl_close($ch);
return $file_contents;
}
再附赠图片转ba//获取图片的Base64编码
function imgToBase64($file){
//$file:图片地址
//Filetype: JPEG,PNG,GIF
// $file = "encode.jpg";
if($fp = fopen($file,"rb", 0))
{
$gambar = fread($fp,filesize($file));
fclose($fp);
$base64 = chunk_split(base64_encode($gambar));
// 输出
// $encode = '<img src="data:image/jpg/png/gif;base64,' . $base64 .'" >';
// echo $encode;
return $base64;
}else{
return false;
}
}
完成!