首先需要到腾讯开发平台创建一个应用,获取到appid 与 appkey拥有了appid与appkey就可以开始编写源码
源码:
include('SDK/API.php');
include('SDK/Configer.php');
include('SDK/HttpUtil.php');
include('SDK/Signature.php');
$path = 'data/order05.png'; // 图片地址
$data = file_get_contents($path); // 获取到文件
$base64 = base64_encode($data); // 使用base64对$data进行编码
$appid = 'XXX'; // appid
$appkey = 'XXX'; // appkey
Configer::setAppInfo($appid, $appkey);
$params = array(
'app_id' => $appid,
'image' => $base64,
'time_stamp' => strval(time()),
'nonce_str' => strval(rand()),
'sign' => '',
);
$params['sign'] = Signature::getReqSign($params, $appkey);
$url = 'https://api.ai.qq.com/fcgi-bin/ocr/ocr_generalocr';
$response = HttpUtil::doHttpPost($url, $params); // 发送请求接收返回值
$newArray = json_decode($response, true); // 转换成php数组
// 返回格式
for($i=0;$i<count($newArray['data']['item_list']);$i++) {
print_r($newArray['data']['item_list'][$i]['itemstring']."<br/>");
}