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"]=>//int (5013683280382131405)//["direction"]=>//Int (0)//[ "Words_result_num"]=>//int (6)//["Words_result"]=>//Array (6) {//[0]=>//Array (2) {// ["Words"]=>//string (3) "Mesh"//["Probability"]=>//Array (3) {//["Variance"] =>//Float (0)//["Average"]=>//float (0.839904)//["Min"]=>// Float (0.839904)//}//}//[1]=>//Array (2) {//["Words"]=>//string (9) "Bad Baidu "//[" Probability "]=>//Array (3) {//[" Variance "]=>//float (0.029807)// ["Average"]=>//float (0.904807)//["Min"]=>//float (0.560053)//}//}// [2]=>//Array (2) {//["Words"]=>//string (44) "Baidu Online Network Technology (Beijing) Co., Ltd."//["Probability"]=& amp;gt;//Array (3) {//["Variance"]=>//float (1.0E-6)//["Average"]=>//float (0.999414)// ["Min"]=>//float (0.996175)//}//}//[3]=>//Array (2) {//["Words"]=&am p;gt;//string ("Baidu Online Network Technology (Beijing) Co., Ltd E"//["Probability"]=>//arr Ay (3) {//["Variance"]=>//float (0.028514)//["Average"]=>//float (0.931889) ["Min"]=>//float (0.226499)//}//}//[4]=>//Array (2) {//["Word S "]=>//string (36)" Beijing Baidu Network News Technology Co., Ltd. "//[" Probability "]=>//Array (3) {//[" Variance "]= >//Float (1.0E-6)//["Average"]=>//float (0.999341)//["Min"]=>// Float (0.997463)//}//}//[5]=>//Array (2) {//["Words"]=>//string (49) " Beijing Baidu Netcom Science Technology Co,, Ltds "//[" Probability "]=>//Array (3) {//[" Variance "]=>//F Loat (0.033526)//["Average"]=>//float (0.896252)//["Min"]=>//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!