Use PHP to call the certificate identification API to identify local images

Time:2022-1-12

Preconditions

Before starting, please make the following preparations:

1. Learn to output “Hello world” in PHP

2. Key for de aggregating data and applying for certificate identification:https://www.juhe.cn/docs/api/…

Operation steps

1. Configure the PHP development environment

2. Create a new folder under the root directory of the corresponding local website and name it card

3. Please prepare an ID photo in JPG format (the picture in this example is from the network) and name it 1 Jpg, put it in the card directory

4. Make sure that PHP is correct for 1 Jpg has read permission (test with fopen (‘1. JPG ‘,’ R ‘) first)

5. Create a new index in the card directory PHP file and enter the following:

<?php
/**
 *Example of certificate identification interface
 *Two methods are provided. Please select the appropriate method according to your PHP version, server environment and other factors
 *The first one is recommended (PHP 5 > = 5.5.0)
 *The ID card picture in the example comes from the network. Using a real ID card picture will have a better recognition effect
 */

header("Content-type:text/html;charset=utf-8");
$config = array(
  'key' = > 'replace me with the key you applied for',
  'url' => ' http://v.juhe.cn/certificates/query.php ', // url address of the aggregated data certificate identification interface
  'cardtype' = > '2', // type of certificate
  'type '= >' image / JPG ', // type of ID picture
);

/*The first way*/
$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 "";
/*/The first way*/

/*The second way*/
$data = array(
  'cardType' => $config['cardType'],
  'key' => $config['key'],
  'pic' => "@1.jpg",
);
post($config['url'], $data);
/*/The second way*/

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. Open the browser to accesshttp://localhost/card/index.php, under normal circumstances, you should see something similar to the following:

{"error_code": "200", "reason": "operation succeeded", "result": {"address": "No. XX, XX village, XX County, XX Province", "reserved": "", "citizenship number": "4201881954082888", "birth": "1954-08-28", "Avatar": "", "name": "XXX", "gender": "female", "nationality": "Han"}}
{"error_code": "200", "reason": "operation succeeded", "result": {"address": "No. XX, XX village, XX County, XX Province", "reserved": "", "citizenship number": "4201881954082888", "birth": "1954-08-28", "Avatar": "", "name": "XXX", "gender": "female", "nationality": "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 document:http://php.net/manual/en/func…

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" : '');
    }
}

?>