Current Location: Home> Latest Articles> How to Integrate Baidu Smart Writing API with PHP: A Detailed Guide and Code Examples

How to Integrate Baidu Smart Writing API with PHP: A Detailed Guide and Code Examples

gitbox 2025-06-15

What is Baidu Smart Writing API?

Baidu Smart Writing API is an AI-powered natural language processing service launched by Baidu, designed to automatically generate grammatically correct and fluent text. This technology is widely used in fields such as news reporting, customer service, and chatbots. In this article, we will explain how to integrate Baidu Smart Writing API with PHP code.

Preparation Before Using the API

1. Register for a Baidu AI Open Platform Account and Enable Smart Writing API

First, you need to register for a Baidu AI Open Platform account. After logging in, navigate to "Products and Services" and find "Smart Writing" to apply for enabling the service. Once the service is enabled, you will receive the API Key and Secret Key, which are required for making API calls.

2. Sending HTTP Requests Using PHP

To integrate with Baidu Smart Writing API, you need to send POST requests via HTTP. PHP’s built-in cURL extension can help us send HTTP requests and receive responses. Below is the PHP code to send requests:


/**
 * @param string $url The request URL
 * @param array $data The request data
 * @return bool|string The response result
 */
function httpPost($url, $data)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_URL, $url);
    $response = curl_exec($ch);
    $error = curl_error($ch);
    curl_close($ch);
    return false;
}

return $response;

}

Integrating Baidu Smart Writing API

1. Preparing Request Data

Before integrating with Baidu Smart Writing API, you need to prepare the request data, which includes the API Key and Secret Key, as well as the text title and content. Below is an example of the request data:


$data = array(
    'title' => 'Welcome to Baidu Smart Writing API',
    'content' => 'By using Baidu Smart Writing API, you can quickly generate fluent and natural language text, suitable for various applications.',
    'apikey' => 'Your API Key',
    'secretkey' => 'Your Secret Key',
    'model' => 'news',  // Options: 'news', 'sentence', or 'poem'
    'prompt' => '0'  // 0: Do not enable leading sentence, 1: Enable leading sentence
);

2. Sending HTTP Request

Next, send the above request data as a POST request to the Baidu Smart Writing API server:


$url = 'https://ai.baidu.com/aidemo';
<p>$response = httpPost($url, $data);</p>
<p>if ($response === false) {<br>
// Request failed<br>
}<br>

3. Handling the Response

Finally, receive and handle the response returned by the Baidu Smart Writing API:


$result = json_decode($response, true);
<p>if ($result['errno'] != 0) {<br>
// Text generation failed<br>
} else {<br>
$content = $result['data']['content'];<br>
// Handle the generated text<br>
}<br>

Conclusion

By using PHP code to integrate with Baidu Smart Writing API, you can easily generate grammatically correct and fluent text. Keep in mind that the generated text must comply with Baidu Smart Writing API's terms of service, or the service may be suspended. Additionally, you can adjust API parameters based on your needs, such as setting the temperature parameter to control the creativity of the generated content.