Current Location: Home> Latest Articles> Quick Guide to Fetching Random Sentences from Baidu Wenxin Yiyan in PHP

Quick Guide to Fetching Random Sentences from Baidu Wenxin Yiyan in PHP

gitbox 2025-06-15

1. What is Wenxin Yiyan

Wenxin Yiyan is an API service that provides random sentences, including a large collection of famous quotes, poems, proverbs from ancient to modern times worldwide. Using random sentences from this API in website footers or article intros can enhance the literary atmosphere and increase user engagement. Therefore, mastering how to call Baidu Wenxin Yiyan API in PHP projects is very practical.

2. Getting the Baidu Wenxin Yiyan API

2.1 Register a Baidu Developer Account

To use Baidu Wenxin Yiyan API, first register a Baidu developer account and create an application. Log into the Baidu AI Cloud Console, click “Create New Application,” fill in the app name, description, and identifier, select services such as “Text Recognition,” and complete the creation process.

After the application is created, you can find it in the application list and note the “App ID” and “API Key,” which are essential for development.

2.2 Apply for the Wenxin Yiyan API

Next, apply for the Wenxin Yiyan service in the Baidu AI Cloud API Marketplace. Search for “Wenxin Yiyan,” click “Apply for Trial,” fill in application details such as app name, description, request body and parameters, then submit. Once approved, you will get the API Key and Secret Key needed for calling the API.

3. PHP Code Implementation to Fetch Baidu Wenxin Yiyan API

3.1 Create a PHP File

Create a PHP file locally or on your server, for example bingyan.php, and include necessary SDK files like:

<span class="fun">require_once 'AipOcr.php';</span>

Define your application variables:

$APP_ID = 'your App ID';
$API_KEY = 'your API Key';
$SECRET_KEY = 'your Secret Key';

Replace these with your own app information and keep your keys secure.

3.2 Send HTTP Request

Use PHP’s cURL extension to send an HTTP request to the Wenxin Yiyan API endpoint. Sample code:

<?php
// Baidu Wenxin Yiyan API endpoint
$url = 'https://cdn.jsdelivr.net/gh/lmk123/cdn-assets/yan.json';
<p>// Generate signature<br>
$timestamp = time();<br>
$nonce = md5(rand(100, 999));<br>
$signature = md5($API_KEY . $timestamp . $nonce . $SECRET_KEY);</p>
<p>// Initialize cURL<br>
$ch = curl_init();<br>
curl_setopt($ch, CURLOPT_URL, $url);<br>
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);<br>
curl_setopt($ch, CURLOPT_POST, 1);<br>
curl_setopt($ch, CURLOPT_HTTPHEADER, array(<br>
'x-appid: ' . $APP_ID,<br>
'x-timestamp: ' . $timestamp,<br>
'x-nonce: ' . $nonce,<br>
'x-signature: ' . $signature,<br>
));</p>
<p>$response = curl_exec($ch);<br>
curl_close($ch);</p>
<p>// Decode JSON data<br>
$data = json_decode($response, true);</p>
<p>// Output random sentence<br>
echo $data['data'][array_rand($data['data'])]['inspire'];<br>
?><br>

The request headers include App ID, timestamp, nonce, and signature for security. Signature is calculated as follows:

<?php
$timestamp = time();
$nonce = md5(rand(100, 999));
$signature = md5($API_KEY . $timestamp . $nonce . $SECRET_KEY);
?>

After sending the request with curl_exec, parse the JSON response with json_decode, then randomly display one inspiring sentence.

4. Summary

Using the Baidu Wenxin Yiyan API to get random sentences in PHP is straightforward. Just register a Baidu developer account, create an app, apply for the Wenxin Yiyan API, and write the PHP code to call it. This service can add literary flavor to your site and also be used for module testing and debugging. PHP developers are encouraged to try integrating this API.