Current Location: Home> Latest Articles> PHP Integration with Baidu Wenxin Yiyan API for Sentence Generation and Sentiment Analysis

PHP Integration with Baidu Wenxin Yiyan API for Sentence Generation and Sentiment Analysis

M66 2025-07-15

Introduction

Baidu Wenxin Yiyan is an API that provides Chinese sentences, which can be generated based on specific types such as motivational, love, friendship, etc. This article will explain how to connect to the Baidu Wenxin Yiyan API using PHP and how to perform sentiment analysis on the generated sentences by calling Baidu's sentiment analysis API.

Preparation

Before starting, you need to complete the following steps:

  • Register a Baidu Developer account, create an application, and obtain the corresponding API Key and Secret Key.
  • Ensure that your PHP environment has the cURL extension installed, as cURL is used for server communication.

Connecting to Baidu Wenxin Yiyan API

First, we need to use the cURL extension to establish a connection with Baidu Wenxin Yiyan API. Below is a simple PHP function that sends a GET request and returns the API's response data. You should replace the API Key and Secret Key with your actual keys.

function callApi($url) {
    $apiKey = "API_KEY";
    $secretKey = "SECRET_KEY";
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
    curl_setopt($curl, CURLOPT_USERPWD, "{$apiKey}:{$secretKey}");
    $response = curl_exec($curl);
    curl_close($curl);
    return $response;
}

With the code above, we can successfully call the Baidu Wenxin Yiyan API and retrieve a sentence of the specified type. Next, let's demonstrate how to call the API.

$url = "https://aip.baidubce.com/rpc/2.0/creation/v1/generate";
$type = "Motivational"; // You can replace it with other types like Love, Friendship, etc.
$requestData = ["type" => $type, "is_profanity" => 1];
$response = callApi($url . "?" . http_build_query($requestData));
$data = json_decode($response, true);
if (isset($data["error_code"])) {
    echo "API request error: " . $data["error_msg"];
} else {
    $sentence = $data["sentence"];
    echo "Generated sentence: " . $sentence;
}

Sentiment Analysis

Once we have a sentence, we can proceed to perform sentiment analysis using Baidu's Sentiment Analysis API. Again, replace the API Key and Secret Key with your own.

function sentimentAnalysis($text) {
    $apiKey = "API_KEY";
    $secretKey = "SECRET_KEY";
    $url = "https://aip.baidubce.com/rpc/2.0/nlp/v1/sentiment_classify";
    $requestData = ["text" => $text];
    $response = callApi($url . "?" . http_build_query($requestData));
    $data = json_decode($response, true);
    if (isset($data["error_code"])) {
        echo "API request error: " . $data["error_msg"];
    } else {
        $positiveProb = $data["items"][0]["positive_prob"];
        $negativeProb = $data["items"][0]["negative_prob"];
        if ($positiveProb > $negativeProb) {
            echo "Sentiment analysis result: Positive";
        } elseif ($positiveProb < $negativeProb) {
            echo "Sentiment analysis result: Negative";
        } else {
            echo "Sentiment analysis result: Neutral";
        }
    }
}

After calling the function, it will print the sentiment analysis result as Positive, Negative, or Neutral.

$sentence = "This is a motivational quote"; // Replace with any sentence
sentimentAnalysis($sentence);

Conclusion

By connecting to the Baidu Wenxin Yiyan API to generate sentences of specific types and using Baidu's Sentiment Analysis API to classify their sentiment, we can easily generate and analyze sentences. This method has a wide range of applications in fields such as public opinion analysis and sentiment monitoring. I hope this article was helpful!