Current Location: Home> Latest Articles> PHP Integration with Baidu Custom Classification API Tutorial: Quickly Implement Text Classification Functionality

PHP Integration with Baidu Custom Classification API Tutorial: Quickly Implement Text Classification Functionality

gitbox 2025-06-13

1. Introduction

Baidu Smart Cloud's Natural Language Processing service provides a Custom Classification API, which can be integrated with PHP. This API allows developers to automatically categorize text, improving content management efficiency. This article will provide a simple and easy-to-follow guide to help developers quickly integrate with the Baidu Custom Classification API.

2. Prerequisites

Before you begin integrating, you need to complete the following setup:

2.1 Create a Baidu Cloud Account

First, visit the Baidu Cloud console, register a Baidu Cloud account, and log in.

2.2 Create a Custom Classification Service Instance

After logging in, go to the Baidu Cloud Natural Language Processing console, select Custom Classification service, and create an instance. Follow the on-screen instructions to complete the setup.

2.3 Obtain API Key and Secret Key

Once the Custom Classification instance is created, navigate to the API Key management page to generate new API Key and Secret Key. These keys will be used in your PHP code for API authentication.

3. PHP Code Integration

After completing the prerequisites, you can begin writing PHP code to integrate with the Baidu Custom Classification API. Below is a code example that you can modify according to your specific requirements.

3.1 Install the SDK

First, install the Baidu PHP SDK using Composer by running the following command:

composer require baidu-aip/sdk

3.2 Include the SDK Class

Include the SDK class in your PHP project:

require_once 'vendor/autoload.php';
use Baidu\Aip\AipNlp;

3.3 Configure API Key and Secret Key

Use your API Key and Secret Key to create an AipNlp instance for API authentication:

$client = new AipNlp('your_app_id', 'your_api_key', 'your_secret_key');

3.4 Call the API to Classify

Use the SDK's provided interface to classify the text:

$result = $client->keyword($text);

4. Conclusion

This article explains how to integrate the Baidu Custom Classification API with PHP. First, set up the necessary configurations and API keys, and then use PHP code to call the API for text classification. Developers can modify and optimize the code based on their specific needs to better serve their project requirements.