Current Location: Home> Latest Articles> How to Integrate Baidu Table OCR API with PHP? A Detailed Tutorial

How to Integrate Baidu Table OCR API with PHP? A Detailed Tutorial

gitbox 2025-06-18

Introduction to Baidu Table OCR API

The Baidu Table OCR API is a powerful service provided by Baidu, which can extract and convert table data from images into an editable table format. This API supports various image formats, offers high-precision recognition, and supports asynchronous calls, ensuring efficient operation in high-concurrency environments.

Register for Baidu OCR Service

To use the Baidu Table OCR API, you first need to register for a Baidu developer account and create a new application to obtain the API keys and identification information. You can complete the registration by visiting the Baidu Developer Center:

https://console.bce.baidu.com/ai/

After registering and logging in, go to the console and create a new application under the "OCR" service. During the application setup, you will need to provide a name, description, and choose the appropriate API service type.

Obtain API Keys and Identification Information

After creating the service, you can find and retrieve the API key (AK) and secret key (SK) from the console. These keys are crucial for API calls, so it’s important to keep them safe and avoid leaking them.

PHP Code Implementation

1. Install Baidu AI SDK

To facilitate the use of Baidu AI API, you can install the Baidu AI PHP SDK via Composer. The installation command is as follows:

composer require baidu-aip-sdk/php-sdk

Once installed, include the Baidu AI SDK autoload class in your PHP project:

require_once 'vendor/autoload.php';

2. Retrieve API Service Keys

Before calling the Baidu Table OCR API, you need to obtain the API key (AK and SK) and create an AipOcr instance:


// Obtain Baidu Cloud API service keys
$app_id = 'YOUR_APP_ID';
$api_key = 'YOUR_API_KEY';
$secret_key = 'YOUR_SECRET_KEY';
// Create AipOcr instance
$client = new AipOcr($app_id, $api_key, $secret_key);

3. Define the Image Path for Recognition

Before calling the API, define the image path for recognition, typically through PHP’s $_FILES variable to get the uploaded image file:


// Get the image file path to recognize
if (isset($_FILES['image'])) {
    $image = $_FILES['image']['tmp_name'];
} else {
    echo 'Please select an image file to recognize';
    exit(1);
}

4. Call the OCR API for Recognition

Next, you can call the Baidu Table OCR API to recognize the table data from the image. Since table recognition can take some time, we will use asynchronous calls:


// Call OCR API to recognize table information
$response = $client->tableRecognitionAsync(file_get_contents($image));
// Get the task ID for table recognition
$task_id = $response['result'][0]['request_id'];
// Get the recognition result
$result = $client->getTableRecognitionResult($task_id);

5. Process the Table Recognition Results

Once the task is completed, you can retrieve and process the table recognition results. The results can be saved in CSV format or another format for easy data import and management, or displayed directly to the front-end user:


// Retrieve the table recognition result
if (isset($result['results'][0]['result_data'])) {
    echo $result['results'][0]['result_data'];
} else {
    echo 'Table recognition failed, please try again later';
}

Conclusion

Following the steps in this article, you can easily integrate PHP with the Baidu Table OCR API to convert table data from images into editable text formats. Be sure to protect your API keys and ensure the security of the file upload process.