Home > php教程 > php手册 > Example of PHP call written by identity document recognition interface

Example of PHP call written by identity document recognition interface

WBOY
Release: 2016-09-27 14:04:28
Original
1543 people have browsed it

Use PHP to call the aggregated data document identification interface to identify local pictures
Prerequisites
Before starting, please make the following preparations:
1. Learn to use PHP to output "Hello World"
2. Go to aggregate data and apply for a KEY dedicated to document identification: https://www.juhe.cn/docs/api/id/153
Operation steps
1. Configure the PHP development environment
2. Create a new folder in the root directory of the corresponding local website and name it: card
3. Please prepare an ID card photo in jpg format (the picture in this example is from the Internet), name it 1.jpg, and put it in the card directory
4. Please make sure that PHP has read permission for 1.jpg (test it with fopen('1.jpg', 'r') first)
5. Create a new index.php file in the card directory and enter the following content: /**<br> * Example of ID recognition interface<br> * Two methods are provided, please choose the appropriate method according to your PHP version, server environment and other factors<br> * It is recommended to use the first one (PHP 5 >= 5.5.0)<br> * The ID card picture in the example comes from the Internet. Using a real ID card picture will have a better recognition effect<br> ​*/<br> <br> header("Content-type:text/html;charset=utf-8");<br> $config = array(<br> 'key' => 'Replace me with the KEY you applied for',<br> //The URL address of the aggregated data document identification interface<br> 'url' => 'http://v.juhe.cn/certificates/query.php',<br> //Type of document, here is the front of the ID card<br> 'type' => 'image/jpg',<br> //Type of ID picture<br> 'cardType' => '2',<br> );<br> <br> /*First way*/<br> $ch = curl_init($config['url']);<br> //$filename <p> Path to the file which will be uploaded.</p><br> //$postname [optional] <p>Name of the file.</p><br> $cfile = curl_file_create('filename.jpg', $config['type'], 'postname.jpg');<br> $data = array(<br> 'cardType' => $config['cardType'],<br> 'key' => $config['key'],<br> 'pic' => $cfile,<br> );<br> curl_setopt($ch, CURLOPT_POST,1);<br> curl_setopt($ch, CURLOPT_POSTFIELDS, $data);<br> //The content has been obtained and has not been output yet. If you do not add the following line, there is no need for echo response<br> //curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);<br> curl_exec($ch);<br> curl_close($ch);<br> <br> /*/First way*/<br> <br> /*Second way*/<br> $data = array(<br> 'cardType' => $config['cardType'],<br> 'key' => $config['key'],<br> 'pic' => "@1.jpg",<br> );<br> post($config['url'], $data);<br> /*/Second way*/<br> <br> function post($url, $data) {<br> $ch = curl_init();<br> curl_setopt( $ch , CURLOPT_POST , true );<br> @curl_setopt( $ch , CURLOPT_POSTFIELDS , $data);<br> curl_setopt($ch, CURLOPT_URL, $url);<br> curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);<br> $response = curl_exec($ch);<br> curl_close($ch);<br> echo $response;<br> }6. Open the browser and visit http://localhost/card/index.php. Under normal circumstances, you should see something similar to the following: {"error_code":"200","reason":"The operation was successful ","result":{"Address":"No. XX, Village XX, XX County, XX Province","Reserved":"","Citizenship Number":"420188195408288888","Birth":"1954-08-28" ,"avatar":"","name":"XXX","gender":"female","ethnicity":"Han"}}<br>{"error_code":"200","reason":"Operation successful","result":{"Address":"No. XX Village, XX County, XX Province","Reserved":"","Citizen Identity Number" :"420188195408288888","birth":"1954-08-28","avatar":"","name":"XXX","gender":"female","ethnicity":"Han"}} 7. If the PHP version is lower than 5.5, but you want to use curl_file_create, please refer to the method provided in the official documentation: http://php.net/manual/en/function.curl-file-create.phpFor PHP < 5.5 :<br /> <br /> <?php<br /> <br /> if (!function_exists('curl_file_create')) {<br /> Function curl_file_create($filename, $mimetype = '', $postname = '') {<br />           return "@$filename;filename="<br />              . ($postname ?: basename($filename))<br />            . ($mimetype ? ";type=$mimetype" : '');<br /> }<br /> }<br /> <br /> ?>

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template