#### 1. 前言 --- 身份證圖片識(shí)別:[https://market.aliyun.com/products/57124001/cmapi010401.html](https://market.aliyun.com/products/57124001/cmapi010401.html) 銀行卡圖片識(shí)別:[https://market.aliyun.com/products/57124001/cmapi016870.html](https://market.aliyun.com/products/57124001/cmapi016870.html) 阿里云OCR印刷文字識(shí)別接口一覽:[https://www.aliyun.com/activity/bigdata/ocrprodpromotionjuly](https://www.aliyun.com/activity/bigdata/ocrprodpromotionjuly) #### 2. 使用示例 --- ```php use app\lib\AliYun; try { // 身份證圖片正面識(shí)別 $result = app(AliYun::class)->idCardOcr($img, 1); // 身份證圖片反面識(shí)別 $result = app(AliYun::class)->idCardOcr($img, 2); // 銀行卡圖片識(shí)別 $result = app(AliYun::class)->bankCardOcr($img); } catch (\Throwable $th) { echo $th->getMessage(); } ``` #### 3. 阿里云接口功能封裝 --- ``` <?php namespace app\lib; /** * 阿里云接口封裝 */ class AliYun { /** * 初始化配置參數(shù) */ public function __construct() { // 阿里云 云市場(chǎng) AppCode $this->appcode = "85ac5eff462e433ea373b2744c7xxxx"; } // +---------------------------------------------- // | OCR 印刷文字識(shí)別 // +---------------------------------------------- /** * 身份證識(shí)別 * * @param string $img 圖片絕對(duì)路徑或URL地址 * @param integer $side 身份證正反面類型:face(1)/back(2) */ public function idCardOcr(string $img, int $side) { $url = "http://dm-51.data.aliyun.com/rest/160601/ocr/ocr_idcard.json"; $body = ['image' => $this->img_base64($img)]; //如果沒有configure字段,config設(shè)為空 //身份證正反面類型:face/back $config = ['side' => $side == 1 ? 'face' : 'back']; count($config) > 0 && $body["configure"] = $config; return $this->curlPost($url, $body); } /** * 銀行卡識(shí)別 * * @param string $img */ public function bankCardOcr(string $img) { $url = "https://yhk.market.alicloudapi.com"; $path = "/rest/160601/ocr/ocr_bank_card.json"; $url = $url . $path; $body = [ 'image' => $this->img_base64($img), 'configure' => [ 'card_type' => true ], ]; return $this->curlPost($url, $body); } /** * curl post 請(qǐng)求封裝 * * @param string $url * @param array $body */ private function curlPost(string $url, array $body) { $method = "POST"; $headers = array(); array_push($headers, "Authorization:APPCODE " . $this->appcode); //根據(jù)API的要求,定義相對(duì)應(yīng)的Content-Type array_push($headers, "Content-Type" . ":" . "application/json; charset=UTF-8"); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); curl_setopt($curl, CURLOPT_FAILONERROR, false); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // curl_setopt($curl, CURLOPT_HEADER, true); if (1 == strpos("$" . $url, "https://")) { curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); } $body = json_encode($body); curl_setopt($curl, CURLOPT_POSTFIELDS, $body); $result = curl_exec($curl); $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); if ($httpCode == 200) { $result = json_decode($result, true); if (isset($result['success']) && $result['success'] === true) { return $result; //識(shí)別成功 } else { throw new \Exception('識(shí)別失敗', 201); } } else { throw new \Exception('圖片錯(cuò)誤', 201); } } /** * 返回圖片的base64或URL地址 * * @param string $path 絕對(duì)路徑或URL地址 */ private function img_base64(string $path) { //對(duì)path進(jìn)行判斷,如果是本地文件就二進(jìn)制讀取并base64編碼,如果是url,則返回 $img_data = ""; if (substr($path, 0, strlen("http")) === "http") { // 網(wǎng)絡(luò)地址圖片 $img_data = $path; } else { // 絕對(duì)地址圖片 if ($fp = fopen($path, "rb", 0)) { $binary = fread($fp, filesize($path)); // 文件讀取 fclose($fp); $img_data = base64_encode($binary); // 轉(zhuǎn)碼 } else { throw new \Exception('圖片不存在', 201); } } return $img_data; } } ```