用PHP調用證件識別介面識別本地圖片

來源:互聯網
上載者:User

標籤:cfile   result   配置   訪問   manual   htm   jpg   應該   file   

前置條件

在開始前,請作如下準備:
1.學會用PHP輸出“Hello World” 
2.去彙總資料申請證件識別專用的KEY:https://www.juhe.cn/docs/api/id/153

操作步驟

1.配置好PHP開發環境 
2.在相應的以磁碟為基礎的網站根目錄下建立一個檔案夾並命名為:card 
3.請準備一張jpg格式的身份證照片(本樣本中的圖片來自網路),並命名為1.jpg,放在card目錄 
4.請務必確保PHP對1.jpg有讀許可權(先用fopen(‘1.jpg’, ‘r’)測試一下) 
5.在card目錄建立一個index.php檔案,並輸入以下內容:

<?php/** * 證件識別介面樣本 * 提供兩種方式,請根據您的PHP版本、伺服器環境等因素選擇適合的方式 * 推薦使用第一種(PHP 5 >= 5.5.0) * 樣本中的身份證圖片來自網路,用真實的身份證圖片會有更佳的識別效果 */header("Content-type:text/html;charset=utf-8");$config = array(  ‘key‘ => ‘將我替換成您申請的KEY‘,  ‘url‘ => ‘http://v.juhe.cn/certificates/query.php‘, //彙總資料證件識別介面的URL地址  ‘cardType‘ => ‘2‘, //證件的類型  ‘type‘ => ‘image/jpg‘, //證件圖片的類型);/*第一種方式*/$ch = curl_init($config[‘url‘]);$cfile = curl_file_create(‘1.jpg‘, $config[‘type‘], ‘1.jpg‘);$data = array(  ‘cardType‘ => $config[‘cardType‘],  ‘key‘ => $config[‘key‘],  ‘pic‘ => $cfile,);curl_setopt($ch, CURLOPT_POST,1);curl_setopt($ch, CURLOPT_POSTFIELDS, $data);curl_exec($ch);curl_close($ch);echo "";/*/第一種方式*//*第二種方式*/$data = array(  ‘cardType‘ => $config[‘cardType‘],  ‘key‘ => $config[‘key‘],  ‘pic‘ => "@1.jpg",);post($config[‘url‘], $data);/*/第二種方式*/function post($url, $data) {  $ch = curl_init();  curl_setopt( $ch , CURLOPT_POST , true );  @curl_setopt( $ch , CURLOPT_POSTFIELDS , $data);  curl_setopt($ch, CURLOPT_URL, $url);  curl_exec($ch);  curl_close($ch);}

6.開啟瀏覽器,訪問http://localhost/card/index.php,正常情況下你應該看到類似下面的內容:

{"error_code":"200","reason":"操作成功","result":{"住址":"XX省XX縣XX村XX號","保留":"","公民身份號碼":"420188195408288888","出生":"1954-08-28","頭像":"","姓名":"XXX","性別":"女","民族":"漢族"}}{"error_code":"200","reason":"操作成功","result":{"住址":"XX省XX縣XX村XX號","保留":"","公民身份號碼":"420188195408288888","出生":"1954-08-28","頭像":"","姓名":"XXX","性別":"女","民族":"漢族"}}

7.如果PHP版本低於5.5,但是又想用curl_file_create,請參考文檔提供的方法:http://php.net/manual/en/function.curl-file-create.php

For PHP < 5.5: <?php if (!function_exists(‘curl_file_create‘)) {    function curl_file_create($filename, $mimetype = ‘‘, $postname = ‘‘) {        return "@$filename;filename="            . ($postname ?: basename($filename))            . ($mimetype ? ";type=$mimetype" : ‘‘);    }} ?>

  

 

用PHP調用證件識別介面識別本地圖片

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.