Current Location: Home> Latest Articles> PHP Integration with Baidu Semantic Parsing API: A Step-by-Step Guide

PHP Integration with Baidu Semantic Parsing API: A Step-by-Step Guide

gitbox 2025-06-18

1. Overview

Baidu Semantic Parsing is an AI-based natural language processing technology that converts input natural language into machine-readable language for further processing. This guide will show you how to integrate the Baidu Semantic Parsing API with PHP.

2. Prerequisites

2.1 Baidu Account

To use the Baidu Semantic Parsing API, you must have a Baidu account. If you don't have one, you can register on the Baidu official website.

2.2 API Key and Secret Key

Before using the Baidu Semantic Parsing API, you need to obtain an API Key and Secret Key. You can create an application on Baidu Cloud (https://cloud.baidu.com/), and once the application is created, you will receive your API Key and Secret Key.

3. Steps to Integrate Baidu Semantic Parsing API with PHP

3.1 Generate Signature

In PHP, we can generate the signature using the following code:

            
$params = array(
    'text' => 'Text to be parsed',
    'access_token' => 'xxx',
    'timestamp' => time(),
    'nonce' => mt_rand(),
);
ksort($params);
$str = '';
foreach ($params as $key => $value) {
    $str .= $key . '=' . urlencode($value) . '&';
}
$str .= 'app_secret=' . APP_SECRET;
$params['sign'] = strtoupper(md5($str));
            

In this example, $params contains the request parameters: 'text' is the text to be parsed, 'access_token' is the API access token, 'timestamp' is the current timestamp, and 'nonce' is a random number.

The function ksort() sorts the array by its keys.

urlencode() encodes special characters for URL compatibility.

'app_secret' refers to your application's Secret Key.

strtoupper() converts the string to uppercase letters.

md5() hashes the string using MD5.

3.2 Send Request

The common method to send HTTP requests in PHP is by using the cURL library. Here's the code to send the request:


$url = 'https://aip.baidubce.com/rpc/2.0/nlp/v1/ecnet';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json; charset=UTF-8'
));
$response = curl_exec($ch);
curl_close($ch);
            

In this code, $url is the API endpoint and $params are the request parameters. The curl_setopt() function is used to set various options for the cURL request:

  • CURLOPT_URL: The URL for the request.
  • CURLOPT_POST: Set the request method to POST.
  • CURLOPT_RETURNTRANSFER: Return the response as a string.
  • CURLOPT_POSTFIELDS: Set the request parameters.
  • CURLOPT_HTTPHEADER: Set the request headers.

3.3 Parse the Response

To parse the response, use the following code:


$result = json_decode($response, true);
if (isset($result['error_code'])) {
    echo 'Error Code: ' . $result['error_code'];
    echo 'Error Message: ' . $result['error_msg'];
} else {
    echo 'Tags: ' . implode(',', $result['item']);
    echo 'Category: ' . $result['type'];
}
            

Here, $response is the response from the API, and json_decode() converts the JSON response into a PHP array.

If the array contains an 'error_code', the API call has failed. You can display the error code and message.

If the array contains 'item' and 'type', the API call was successful. You can display the tags and category information.

4. Conclusion

This guide provided a detailed overview of how to integrate the Baidu Semantic Parsing API with PHP, covering the steps of generating the signature, sending the request, and parsing the response. We hope this tutorial helps you integrate Baidu's powerful AI capabilities into your PHP applications effectively.