Current Location: Home> Latest Articles> Implementation and Sample Code for Integrating Baidu Custom Classification API with PHP

Implementation and Sample Code for Integrating Baidu Custom Classification API with PHP

M66 2025-07-12

Implementation and Sample Code for Integrating Baidu Custom Classification API with PHP

The Baidu Custom Classification API offers developers a convenient tool for automatic classification of data such as text and images. This article will guide you through how to connect to the Baidu Custom Classification API using PHP, and provide code examples to help you implement data classification functionality.

Applying for Baidu Custom Classification API

First, you need to apply for API access permissions on the Baidu Developer Platform. You can refer to Baidu’s official documentation for the application process, and obtain the API access token and other related information.

Preparation

Before starting to write code, make sure your development environment is set up with PHP and that the curl extension is enabled. You can check if the curl extension is installed by running the following command:

php -m | grep curl

If it returns “curl”, it means the curl extension is installed and enabled.

Writing PHP Code to Integrate the API

Next, follow the steps below to implement PHP integration with the Baidu Custom Classification API:

Define Required Parameters

$access_token = 'YOUR_ACCESS_TOKEN';  // Access token for Baidu Custom Classification API
$url = 'https://aip.baidubce.com/rpc/2.0/unit/service/classify';  // API request URL
$method = 'POST';  // Request method
$headers = array('Content-Type: application/json');  // Request headers

Here, $access_token is the access token you obtained from Baidu Developer Platform, $url is the API endpoint, $method is the request method (POST in this case), and $headers specifies the data format for the request.

Construct Request Data

$data = array(
    'text' => 'Text to be classified',
    'type' => 'custom',  // Use custom classification
    'top_num' => 5  // Number of classification results to return
);

$data is an associative array containing the text to be classified, the classification type, and the number of results to return.

Send the Request

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);

The above code uses PHP’s curl library to send the HTTP request and stores the response in the $result variable.

Process the Response

$result_array = json_decode($result, true);  // Decode the returned JSON string into an associative array
if (isset($result_array['error_code'])) {
    echo 'An error occurred: ' . $result_array['error_msg'];
} else {
    $classification = $result_array['results'][0]['name'];
    echo 'Classification result: ' . $classification;
}

The code above converts the JSON response into an associative array, checks if there is any error message, and if none, parses and outputs the classification name.

Summary

Following the steps introduced in this article, PHP developers can easily integrate the Baidu Custom Classification API. The sample code demonstrates how to define necessary request parameters, send requests, and handle the response. You can extend the code to add error handling, logging, and other features to enhance robustness and security.

This is just a simple example; in practical use, you may need to optimize the code according to your specific business logic. For detailed API parameter descriptions and additional functionalities, please refer to the official Baidu Custom Classification API documentation.