Home >Backend Development >PHP Tutorial >How to connect Baidu Wenxin Yiyan API with PHP to obtain random sentences and generate web page abstracts

How to connect Baidu Wenxin Yiyan API with PHP to obtain random sentences and generate web page abstracts

WBOY
WBOYOriginal
2023-08-12 08:44:01906browse

How to connect Baidu Wenxin Yiyan API with PHP to obtain random sentences and generate web page abstracts

How to connect PHP to Baidu Wenxin Yiyan API to obtain random statements and generate web page abstracts

With the rapid development of the Internet, web page abstracts have become a way for users to obtain information. One of the important ways. The use of random sentences can add some interest and flexibility to the web page summary. This article will introduce how to use PHP to connect to Baidu Wenxin Yiyan API, and combine HTML and CSS technology to generate a web page summary with random sentences.

Step one: Apply for a Baidu developer account and create an application
Before we start, we need to have a Baidu developer account and create an application in the account. The specific operations are as follows:

  1. Visit the official website of Baidu AI Open Platform (https://ai.baidu.com/), click "Console" in the upper right corner to enter the console page;
  2. Log in with a Baidu account, enter the console and click the "Create Application" button;
  3. Fill in the application name, application description and select the corresponding computer vision category on the create application page, submit and save;
  4. After successful creation, find "API Key" and "Secret Key" under the "Web Page Summary" category of the console. This information will be used in PHP code to connect to Baidu API.

Step 2: Write PHP code
// API Key and Secret Key of Baidu Open Platform
$apiKey = "your_api_key";
$secretKey = "your_secret_key";

// URL of the random statement API
$apiUrl = "https://aip.baidubce.com/rpc/2.0/nlp/v1/sentence_emb";

//The number of random sentences to be obtained
$numOfSentences = 5;

//Loop to request random sentences
for($i = 0; $i

// 生成请求体
$requestBody = array(
    "query" => "随机语句"
);

// 发送POST请求到百度API
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($requestBody));

// 设置API Key和Secret Key
$headers = array(
    "Content-Type: application/json",
    "charset: UTF-8"
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$auth = base64_encode($apiKey . ":" . $secretKey);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Basic ' . $auth));

// 执行API请求并获取结果
$result = curl_exec($ch);

// 处理返回结果
if($result === FALSE){
    echo "请求百度API失败:" . curl_error($ch);
} else {
    // 解析返回结果
    $result = json_decode($result, true);
    $sentence = $result["sentences"][0]["sentence"];
    
    // 输出随机语句
    echo "<p>" . $sentence . "</p>";
}

// 关闭curl资源
curl_close($ch);

}
?>

Step 3: Create HTML and CSS files


<meta charset="UTF-8">
<title>网页摘要生成</title>
<style>
    body {
        font-family: Arial, sans-serif;
    }
    
    .container {
        width: 800px;
        margin: 0 auto;
    }
    
    p {
        font-size: 20px;
        margin-bottom: 10px;
    }
</style>


<div class="container">
    <?php include "generate_summary.php"; ?>
</div>


Step 4: Embed the PHP code into the HTML file
Save the PHP code for generating random statements as generate_summary.php and embed it into the HTML file.

Step 5: Run the program that generates web page abstracts
Upload the written HTML file to the Web server, access the file through the browser, and then run the program that generates web page abstracts.

Through the above steps, we can use PHP to connect to Baidu Wenxin Yiyan API to obtain random sentences and generate web page abstracts with random sentences. Such an implementation can not only increase the interest of web page abstracts, but also enable users to better obtain and understand the content of web pages.

The above is the detailed content of How to connect Baidu Wenxin Yiyan API with PHP to obtain random sentences and generate web page abstracts. 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