首先需要到騰訊開發平台創建一個應用,獲取到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/>");
}