Implementation of the method of detecting orc in PHP ID card

Source: Internet
Author: User
Tags form post readfile
This article mainly introduces the PHP identity card recognition Orc method implementation, has a certain reference value, now share to everyone, the need for friends can refer to

First create an HTML and transfer it to a PHP file in JSON format

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> 

Create another upload.php

<?phpheader ("content-type:text/html; Charset=utf-8 ")/** * Base64 image upload * @param $base 64_img * @return Array */$base 64_img = Trim ($_post[' img '); $up _dir = ' Uploa D/';//stored in the current directory under the upload folder $fi_dir = ' ok_upload/';//stored in the current directory under the Upload folder if (!file_exists ($up _dir)) {mkdir ($up _dir,0777) ;}    if (Preg_match ('/^ (data:\s*image\/(\w+); base64,)/', $base 64_img, $result)) {$type = $result [2]; if (In_array ($type, Array (' pjpeg ', ' jpeg ', ' jpg ', ' gif ', ' BMP ', ' png '))) {$new _file = $up _dir.date (' Ymdhis_ ').        $type; if (file_put_contents ($new _file, Base64_decode (Str_replace ($result [1], ", $base 64_img)))) {$img _path = Str_replace (' .. /.. /: ', ', $new _file); $path = ' upload/'; $data = file_get_contents ($img _path); $base = Base64_encode ($data); $appkey = '                Lzju1grfwh6uadx2 '; $params = Array (' app_id ' = ' 1106920947 ', ' image ' = + $base 64, ' Card_type ' = ' 0 ', ' time_stamp ' + strval (Time ()), ' nonce_str ' = Strval ( RanD ()), ' sign ' + ', ', ' $params [' sign '] = Getreqsign ($params, $appkey);//execute API call to ' HTTPS://API '.            AI.QQ.COM/FCGI-BIN/OCR/OCR_IDCARDOCR ';//identity card identification Ocr$response = Dohttppost ($url, $params); echo $response;d ie; $arr = Json_decode ($response, true); $photo = Base64_decode ($arr [' Data '] [' image ']); if (!file_exists ($fi _dir)) {mkdir ($fi _dir,0777);}    $type = ' jpg '; if (In_array ($type, Array (' pjpeg ', ' jpeg ', ' jpg ', ' gif ', ' BMP ', ' png '))) {$new _file = $fi _dir.date (' Ymdhis_ ').        $type; if (file_put_contents ($new _file, Str_replace ($result [1], ", $photo)) {$img _paths = Str_replace ('). /..            /: ', ', $new _file); echo ' picture processing succeeded </br> ';        }else{echo ' picture processing failed </br> ';        }}}else{echo ' picture upload failed </br> ';    }}else{//File type error echo ' picture upload type error '; }}else{//File error echo ' file error ';}
//getreqsign: Calculate request signature based on interface request parameters and application key//-$params: Interface Request parameters (Special note: Different interfaces, parameter pairs generally do not , please refer to the specific interface requirements)//-$appkey: Apply key//Return data//-signature result function getreqsign ($params/* Associative array */, $appkey/* String */) {//1. Dictionary liters    Sequential sequencing ksort ($params); 2.    Press the URL key value pair $str = '; foreach ($params as $key = + $value) {if ($value!== ") {$str. = $key. '=' . UrlEncode ($value).        ' & '; }}//3. Stitching app_key $str. = ' app_key= '.    $appkey; 4.    MD5 operation + Convert uppercase, GET request Signature $sign = Strtoupper (MD5 ($STR)); return $sign;} 
Dohttppost: Performing a POST request and retrieving the response result//Parameter description//-$url: Interface Request address//-$params: Full Interface Request parameters (Special note: Different interfaces, parameters for the general difference, please specify the interface requirements)//return data/    /-return False indicates failure, otherwise the HTTP body part function Dohttppost ($url, $params) {$curl = Curl_init () that the API successfully returned;    $response = false;        do {//1. Set HTTP URL (API address) curl_setopt ($curl, Curlopt_url, $url); 2.        Set HTTP HEADER (Form POST) $head = Array (' content-type:application/x-www-form-urlencoded ');        curl_setopt ($curl, Curlopt_httpheader, $head); 3.        Sets the HTTP BODY (URL key value pair) $body = Http_build_query ($params);        curl_setopt ($curl, Curlopt_post, true);        curl_setopt ($curl, Curlopt_postfields, $body); 4.        Invoke API, get response result curl_setopt ($curl, Curlopt_header, false);        curl_setopt ($curl, Curlopt_nobody, false);        curl_setopt ($curl, Curlopt_returntransfer, true);        curl_setopt ($curl, Curlopt_ssl_verifyhost, true);        curl_setopt ($curl, Curlopt_ssl_verifypeer, false); $Response = curl_exec ($curl);            if ($response = = = False) {$response = false;        Break        } $code = Curl_getinfo ($curl, Curlinfo_http_code);            if ($code! =) {$response = false;        Break    }} while (0);    Curl_close ($curl); return $response;}

So we can identify the information on the ID card.

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.