Current Location: Home> Latest Articles> How to Integrate Baidu Smart Classification API with PHP: A Comprehensive Guide

How to Integrate Baidu Smart Classification API with PHP: A Comprehensive Guide

M66 2025-07-27

Introduction

With the continuous advancement of artificial intelligence technology, smart classification is widely applied in various industries, especially in the field of natural language processing. Baidu's Smart Classification API is a powerful tool that automatically classifies incoming text, helping developers efficiently process text data. This article introduces how to integrate Baidu Smart Classification API using PHP, providing simple code examples to help developers quickly implement this feature.

Preparation

Before starting, you need to do the following preparations:

  • Register and log in to your Baidu Developer account.
  • Create an application and obtain your API Key and Secret Key.
  • Ensure that your PHP environment has the CURL extension installed.

Integrating Baidu Smart Classification SDK

To use the Baidu Smart Classification API, you first need to integrate Baidu Cloud's NLP SDK. The steps are as follows:

  • Visit the Baidu Smart Cloud Developer Center and download the PHP version of the NLP SDK.
  • Unzip the downloaded package and copy the AipNlp.php file to your project.

Code Example

Here is a simple code example demonstrating how to use PHP to call Baidu's Smart Classification API for text classification:

<?php
require_once 'AipNlp.php'; // Import Baidu Cloud NLP SDK

// Set APPID/AK/SK
const APP_ID = 'your_app_id';
const API_KEY = 'your_api_key';
const SECRET_KEY = 'your_secret_key';

// Instantiate Baidu Cloud NLP SDK
$client = new AipNlp(APP_ID, API_KEY, SECRET_KEY);

// Input the texts to be classified
$texts = array(
    'Hello, I am an engineer.',
    'The weather was great last night, and I went to the movies with my friends.',
    'The plot of this movie is fantastic, highly recommend it to everyone.'
);

// Call the classification API for text classification
$result = $client->lexer($texts);

// Parse the returned results
if (!empty($result['items'])) {
    foreach ($result['items'] as $item) {
        $text = $texts[$item['sent_idx']];
        $category = $item['item'];
        echo "Text: {$text}";
        echo "Category: {$category}";
    }
}
?> 

Running the Example Code

Save the above code as demo.php, and set your correct APPID, API Key, and Secret Key. Then, run the following command in the terminal or command line:

php demo.php

After executing the command, you will see each text and its corresponding classification result.

Conclusion

By following the steps above, you can easily integrate Baidu Smart Classification API with PHP. This API is ideal for projects that require fast text classification and can significantly improve text processing efficiency. However, the accuracy of the results depends on the quality of the input text, so you may need to optimize your data based on specific needs. Mastering this technology will add powerful text processing capabilities to your projects.

I hope this article helps you. If you have any questions, feel free to leave a comment and discuss.