Current Location: Home> Latest Articles> Complete Tutorial and Example for Integrating Baidu OCR License Plate Recognition API with PHP

Complete Tutorial and Example for Integrating Baidu OCR License Plate Recognition API with PHP

gitbox 2025-06-12

Introduction

With the increasing number of vehicles, license plate recognition technology has become widely used in fields such as security, traffic management, parking management, and intelligent transportation. Among these technologies, Optical Character Recognition (OCR) is one of the most commonly used methods. Baidu OCR, with its high accuracy and fast recognition speed, has become one of the most popular choices in the market.

This article will guide you step by step on how to use PHP to integrate Baidu OCR License Plate Recognition API and achieve license plate recognition functionality.

Steps

1. Obtain Baidu OCR API Key and Secret Key

First, go to the Baidu Intelligent Cloud website, register, and log in. In the "My Products" page, choose OCR, and create an application in the "Management Console." Once the application is created, you can obtain the API Key and Secret Key.

2. Install Baidu SDK

Before development, we need to install the Baidu PHP SDK. You can download the SDK from Baidu AIP's official GitHub repository here: [Baidu AIP PHP SDK](https://github.com/Baidu-AIP/sdk-php).

3. Write the Integration Code

After installing the SDK, we need to include it in our project and create an instance of the AipOcr class. Here’s a simple code example:


require_once 'AipOcr.php';  // Include Baidu OCR SDK

// Create AipOcr object
$config = [
    'app_id' => 'your_app_id',
    'api_key' => 'your_api_key',
    'secret_key' => 'your_secret_key',
];
$client = new AipOcr($config);

Next, we’ll upload the license plate image and call the Baidu OCR license plate recognition API. After the recognition is complete, the license plate number will be output:


// Upload the license plate image
$image = file_get_contents('path/to/image.jpg');

// Call the API for license plate recognition
$res = $client->licensePlate($image);

// Output the recognition result
echo $res['words_result']['number'];

4. Full Code Example

Here is the complete PHP code example that demonstrates how to integrate Baidu OCR License Plate Recognition API:


require_once 'AipOcr.php';  // Include Baidu OCR SDK

// Create AipOcr object
$config = [
    'app_id' => 'your_app_id',
    'api_key' => 'your_api_key',
    'secret_key' => 'your_secret_key',
];
$client = new AipOcr($config);

// Upload the license plate image
$image = file_get_contents('path/to/image.jpg');

// Call the API for license plate recognition
$res = $client->licensePlate($image);

// Output the recognition result
echo $res['words_result']['number'];

Conclusion

This article introduced how to integrate Baidu OCR License Plate Recognition API using PHP. Through this tutorial, you are now able to easily implement automatic license plate recognition using simple PHP code. This technology is widely applied in intelligent transportation, parking management, and law enforcement, helping to improve efficiency and security.