Home >Backend Development >PHP Tutorial >PHP connects Baidu Wenxin Yiyan API to achieve random sentence generation sample code

PHP connects Baidu Wenxin Yiyan API to achieve random sentence generation sample code

WBOY
WBOYOriginal
2023-08-14 08:25:061427browse

PHP connects Baidu Wenxin Yiyan API to achieve random sentence generation sample code

Sample code for PHP to connect to Baidu Wenxin Yiyan API to achieve random sentence generation

Overview:
Baidu Wenxin Yiyan is a program that provides famous quotes, API interface for generating random sentences such as ancient poems and chicken soup. By connecting to this API, we can add some interesting sentences to our websites or applications to bring a better experience to users. This article will introduce how to use PHP to connect to Baidu Wenxin Yiyan API and provide corresponding sample code.

Step 1: Apply for API interface
First, you need to register an account on Baidu Open Cloud official website (https://cloud.baidu.com) and create a new project. Find the corresponding "smart technology" in the project, enter Baidu Wenxinyiyan's interface application page, click "Open Now", and the system will assign you an API Key and Secret Key.

Step 2: Write PHP code
Before starting, please make sure that your PHP environment has the cURL extension installed. Next we start writing code.

First create a new PHP file, named index.php, and introduce the following code into the file:

<?php

// 设置API Key和Secret Key
$apiKey = 'your_api_key';
$secretKey = 'your_secret_key';

// 生成请求Token
$token = sha1($apiKey . $secretKey . time());

// 设置请求URL
$url = 'https://apiwx.open.baidu.com/wuxin/getorinbaseapi?appid=' . $apiKey . '&token=' . $token;

// 初始化cURL
$ch = curl_init();

// 配置cURL
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

// 执行请求
$response = curl_exec($ch);

// 处理响应
if ($response === false) {
    echo 'Error: ' . curl_error($ch);
} else {
    $result = json_decode($response, true);
    if (isset($result['ret_code']) && $result['ret_code'] == 0) {
        echo $result['sentence'];
    } else {
        echo 'Error: Failed to get sentence.';
    }
}

// 关闭cURL
curl_close($ch);

In the above code, replace your_api_key and your_secret_keyReplace with the API Key and Secret Key you obtained in step 1. The code sends a request to Baidu Wenxinyiyan's API interface through cURL and returns the sentence content.

Step 3: Run the code
Upload the written index.php file to your website server (make sure the server supports PHP), and then access the URL of the file in the browser. See randomly generated sentences.

Conclusion:
By connecting to Baidu Wenxin Yiyan API, we can easily realize the function of random sentence generation. In practical applications, you can further process and display the returned sentences according to your own needs, such as displaying a daily inspirational sentence on the homepage of the website, or randomly displaying an ancient poem in a WeChat applet. Hope this article is helpful to you!

The above is the detailed content of PHP connects Baidu Wenxin Yiyan API to achieve random sentence generation sample code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn