php ci extends Baidu image text recognition SDK method (applicable to PHP ci Xiaobai) WeChat payment, Alipay payment and other extensions are also similar

PHP-ci version: 3.1.8

Baidu text recognition PHP sdk version: 2.2.2


Baidu text recognition document address (the document has the address of OCR PHP SDK download) https://cloud.baidu.com/doc/OCR/OCR-PHP-SDK.html#.E6.96.B0.E5.BB. BAAipOcr


After the download is complete, unzip the file to the application/libraries of PHP CodeIgniter (I built a directory under the libraries directory for separate management)



figure 1

If you create folder management in libraries separately like me, you need to make simple changes in the file introduction in the program; as shown in Figure 2


figure 2

If all the files and directories of the OCR PHP SDK are directly placed in the libraries directory, the above file paths can be left unchanged.



Here comes the point:

To change  the construction method of the lib/AipBase.php file of the OCR PHP SDK (approximately between lines 50 and 80) 

public function __construct($dataarr){
        $this->appId = trim($dataarr['appId']);
        $this->apiKey = trim($dataarr['apiKey']);
        $this->secretKey = trim($dataarr['secretKey']);
        /*//The original constructor with three parameters is now passed as an array, the code is as above
         $this->appId = trim($appId);
        $this->apiKey = trim($apiKey);
        $this->secretKey = trim($secretKey);
*/
        $this->isCloudUser = null;
        $this->client = new AipHttpClient();
        $this->version = '2_2_2';
        $this->proxies = array();
    }


Why change it this way? (PHP ci official document address http://codeigniter.org.cn/user_guide/general/creating_libraries.html) Because PHP ci's

$this->load->library()//When loading the class library, the second parameter carries an array, so we need to use the three parameters of the above construction method as one parameter

This is simply changed. ready to use. The point is to remember to change the AipBase.php file.

Constructor of the AipBase class. .

The other is to load the class library in PHP ci, the code is as follows:
defined('BASEPATH') OR exit('No direct script access allowed');

class Baiduocr extends CI_Controller {

	
	public function index()
	{
		$params = array('appId'=>'fill in your own', 'apiKey'=>' fill in your own ', 'secreKey'=>' fill in your own ');
		$this->load->library('AipOcr',$params);//Here you can create a config file for processing according to the official PHPci documentation given above
		$url ='http://wwwww.s21i.faiusr.com/4/11111111.png';//图片URL
		$data = $this->aipocr->basicGeneralUrl($url);//Choose the method of the corresponding function you need here
		print_r($data);
	}
}

In this way, the data can be identified (of course, the identified results will be processed according to your own needs)
, and you can further understand the creation class library of PHP ci. . .



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324521839&siteId=291194637