Current Location: Home> Latest Articles> How to Use PHP to Integrate Baidu Natural Language Understanding API for Text Analysis

How to Use PHP to Integrate Baidu Natural Language Understanding API for Text Analysis

M66 2025-07-03

How to Use PHP to Integrate Baidu Natural Language Understanding API for Text Analysis

With the rapid advancement of artificial intelligence technologies, Natural Language Processing (NLP) has become an integral part of many applications. Baidu's Natural Language Understanding API is a powerful tool widely used in text classification, sentiment analysis, entity recognition, and more. This article will explain how to integrate Baidu's NLP API with PHP and provide relevant code examples.

Preparation Steps

Before starting, you need to register a Baidu Developer account and create an application on Baidu Cloud to obtain your API Key and Secret Key. These keys will be used for API calls to ensure the security of data transmission.

Step 1: Get the Access Token

Baidu's NLP API requires an Access Token for authentication. First, we need to use Baidu's Token API to get the Access Token. Here is an example code using PHP and the curl library to send an HTTP request:


<?php
// Define the API URL
$url = 'https://aip.baidubce.com/oauth/2.0/token';

// Define API Key and Secret Key
$apiKey = 'your_api_key';
$secretKey = 'your_secret_key';

// Define parameters array
$params = array(
    'grant_type' => 'client_credentials',
    'client_id' => $apiKey,
    'client_secret' => $secretKey
);

// Send POST request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);

// Handle the response
$result = json_decode($response, true);
if (isset($result['access_token'])) {
    $accessToken = $result['access_token'];
}
?>

This code defines the API URL, sends a POST request with the API Key and Secret Key, and returns the Access Token if successful.

Step 2: Call the Natural Language Understanding API

After obtaining the Access Token, you can use it to call Baidu's NLP API. The following code shows how to use the Access Token for text tokenization:


<?php
// Call the API for text analysis
$url = 'https://aip.baidubce.com/rpc/2.0/nlp/v1/lexer';
$params = array(
    'text' => 'This is a test text',
    'access_token' => $accessToken
);

// Send POST request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);

// Handle the response
$result = json_decode($response, true);
if (isset($result['items'])) {
    foreach ($result['items'] as $item) {
        echo $item['item'] . ' ';
    }
}
?>

The above code calls Baidu's tokenization API, sends a test text, and outputs the tokenization result.

Key Considerations

The parameters in the example code above are basic and simplified. In real-world scenarios, you may need to adjust parameters based on specific business needs. Additionally, Baidu's NLP API supports various features like sentiment analysis, entity recognition, and more, which you can use depending on your requirements.

Conclusion

With the information provided in this article, you should now understand how to integrate Baidu's Natural Language Understanding API with PHP for text processing. In practice, you may need to combine more API features based on your business scenarios. We hope this article is helpful to you!