POST /ocr/cz HTTP/1.1
Host: ocr-cz.qiniuapi.com
Content-Type: application/json
Authorization: QiniuToken头部信息//依蓝云(七牛)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;
}
}完成!ThinkPHP 是一个免费开源的,快速、简单的面向对象的 轻量级PHP开发框架 ,创立于2006年初,遵循Apache2开源协议发布,是为了敏捷WEB应用开发和简化企业应用开发而诞生的。ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,也注重易用性。并且拥有众多的原创功能和特性,在社区团队的积极参与下,在易用性、扩展性和性能方面不断优化和改进,已经成长为国内最领先和最具影响力的WEB应用开发框架,众多的典型案例确保可以稳定用于商业以及门户级的开发。