PHP implementation calls Baidu's OCR character recognition interface

Source: Internet
Author: User
This article mainly introduces the PHP implementation of the call Baidu OCR text recognition interface, has a certain reference value, now share to everyone, there is a need for friends can refer to

1, through the login Baidu API to obtain the text recognition interface

Https://ai.baidu.com/tech/ocr/general

Specific interface call parameters according to Baidu's official document

Https://ai.baidu.com/docs#/OCR-API/top

2, the next is to call the interface to use and processing data. I use the most common interface. 50,000 per day, no concurrency guaranteed.

A bit for my Code section:

<?php//access_token Call function request_post ($url = ', $param = ') {if (empty ($url) | | empty ($param)) {        return false;        } $POSTURL = $url;        $curlPost = $param; $curl = Curl_init ();//Initialize Curl curl_setopt ($curl, Curlopt_url, $POSTURL);//Crawl specified page curl_setopt ($curl, Curlopt_h Eader, 0);//Set Header curl_setopt ($curl, Curlopt_returntransfer, 1);//require the result to be a string and output to the on-screen curl_setopt ($curl, Curl        Opt_post, 1);//post Submission Method curl_setopt ($curl, Curlopt_postfields, $curlPost);                $data = curl_exec ($curl);//Run Curl Curl_close ($curl);    return $data;    } $url = ' Https://aip.baidubce.com/oauth/2.0/token ';    $post _data[' grant_type '] = ' client_credentials ';    $post _data[' client_id '] = ' your API key ';    $post _data[' client_secret '] = ' your secret key ';    $o = ""; foreach ($post _data as $k = + $v) {$o. = "$k =". UrlEncode ($v).    "&"; } $post _data = substr ($o, 0,-1);       $res = Request_post ($url, $post _data);    $access _token = Json_decode ($res, 1) [' Access_token '];    Var_dump ($access _token); Call End//post Request Interface Function index ($access _token) {$data = array (' url ' = = ' http://aip.bdstatic.c Om/portal/dist/1530540600796/ai_images/technology/ocr-general/general/tech-general-original-scanned.png ',//            Your image address//' image ' = ', '//Check the image toward ' detect_direction ' + ' true ',//return confidence        ' Probability ' = ' true ',);        $response = Config ($data, $access _token);    return $response; } function config ($data, $access _token) {//Request URL requires access_token parameter $url = ' https://aip.baidubce.com/rest/        2.0/ocr/v1/general_basic?access_token= '. $access _token;       $header = Array (' content-type:application/x-www-form-urlencoded ');       $response = Tocurl ($url, $header, $data);    return $response; } function Tocurl ($url, $header, $content) {$ch = Curl_init (); if (substr ($url, 0,5) = = ' https ') {curl_setopt ($ch, Curlopt_ssl_verifypeer, false);//Skip certificate Check//curl_seto  PT ($ch, Curlopt_ssl_verifyhost, true);        Check the existence of the SSL encryption algorithm from the certificate} curl_setopt ($ch, Curlopt_returntransfer, true);        curl_setopt ($ch, Curlopt_url, $url);        curl_setopt ($ch, Curlopt_httpheader, $header);        curl_setopt ($ch, Curlopt_post, true);        curl_setopt ($ch, Curlopt_postfields, Http_build_query ($content));        $response = curl_exec ($ch);        if ($error =curl_error ($ch)) {die ($error);        } curl_close ($ch);    return $response;    } $text _json = index ($access _token);    $text _arr = Json_decode ($text _json,1); Var_dump ($text _arr);? >
The last data obtained are://Array (4) {//["log_id"]=&gt;//int (5013683280382131405)//["direction"]=&gt;//Int (0)//[        "Words_result_num"]=&gt;//int (6)//["Words_result"]=&gt;//Array (6) {//[0]=&gt;//Array (2) {// ["Words"]=&gt;//string (3) "Mesh"//["Probability"]=&gt;//Array (3) {//["Variance"]         =&gt;//Float (0)//["Average"]=&gt;//float (0.839904)//["Min"]=&gt;// Float (0.839904)//}//}//[1]=&gt;//Array (2) {//["Words"]=&gt;//string (9) "Bad Baidu         "//[" Probability "]=&gt;//Array (3) {//[" Variance "]=&gt;//float (0.029807)//     ["Average"]=&gt;//float (0.904807)//["Min"]=&gt;//float (0.560053)//}//}// [2]=&gt;//Array (2) {//["Words"]=&gt;//string (44) "Baidu Online Network Technology (Beijing) Co., Ltd."//["Probability"]=& amp;gt;//Array (3) {//["Variance"]=&gt;//float (1.0E-6)//["Average"]=&gt;//float (0.999414)// ["Min"]=&gt;//float (0.996175)//}//}//[3]=&gt;//Array (2) {//["Words"]=&am p;gt;//string ("Baidu Online Network Technology (Beijing) Co., Ltd E"//["Probability"]=&gt;//arr Ay (3) {//["Variance"]=&gt;//float (0.028514)//["Average"]=&gt;//float (0.931889) ["Min"]=&gt;//float (0.226499)//}//}//[4]=&gt;//Array (2) {//["Word S "]=&gt;//string (36)" Beijing Baidu Network News Technology Co., Ltd. "//[" Probability "]=&gt;//Array (3) {//[" Variance "]=         &gt;//Float (1.0E-6)//["Average"]=&gt;//float (0.999341)//["Min"]=&gt;// Float (0.997463)//}//}//[5]=&gt;//Array (2) {//["Words"]=&gt;//string (49) " Beijing Baidu Netcom Science Technology Co,, Ltds "//[" Probability "]=&gt;//Array (3) {//[" Variance "]=&gt;//F Loat (0.033526)//["Average"]=&gt;//float (0.896252)//["Min"]=&gt;//float (0.45208 3)//}//}//}//}

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.