Home > Backend Development > PHP Tutorial > How to connect Baidu Wenxin Yiyan API with PHP to obtain random statements and generate blog summary

How to connect Baidu Wenxin Yiyan API with PHP to obtain random statements and generate blog summary

WBOY
Release: 2023-08-12 17:02:01
Original
919 people have browsed it

How to connect Baidu Wenxin Yiyan API with PHP to obtain random statements and generate blog summary

How to connect PHP to Baidu Wenxin Yiyan API to obtain random sentences and generate blog summaries

With the development of the Internet, blogs have become a way for people to share knowledge and experience important platform. A good blog needs to have an attractive title and exciting content. And generating an attractive blog summary is also key to attracting readers. This article will introduce a method of using PHP to connect to Baidu Wenxin Yiyan API to obtain random sentences and generate blog summaries.

Baidu Wenxin Yiyan is an API service that provides random sentences. It provides a large number of sentences, including philosophy, poetry, movie lines, etc. We can use this API to generate blog summaries and add some literary flavor to the blog.

First, we need to apply for a Baidu developer account, create an application, and obtain the API Key and Secret Key. Then, we can use PHP's cURL library to connect to Baidu Wenxin Yiyan API and obtain random statements.

<?php
    // 定义API请求地址和参数
    $url = 'https://api.lwl12.com/hitokoto/v1?encode=json';
    $params = [
        'encode' => 'json',
    ];

    // 拼接请求URL
    $url .= '&' . http_build_query($params);

    // 初始化cURL
    $ch = curl_init();
    // 设置请求URL
    curl_setopt($ch, CURLOPT_URL, $url);
    // 设置返回结果不直接输出
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    // 发起请求
    $response = curl_exec($ch);
    // 关闭cURL
    curl_close($ch);

    // 解析JSON格式的响应结果
    $result = json_decode($response, true);
    // 获取语句内容
    $sentence = $result['hitokoto'];

    // 输出结果
    echo $sentence;
?>
Copy after login

The above code will connect to Baidu Wenxin Yiyan API and obtain random statements. We can place this code in the blog generation process to get different statements.

Next, we can generate a summary of the blog based on the obtained statements. When generating a summary, we can extract the keywords in the sentence and add some text descriptions. The following is a sample code:

<?php
    // 获取语句
    $sentence = '这是一条随机语句';

    // 提取关键词
    $keywords = ['随机', '语句'];

    // 加上说明文字
    $intro = '本文来自百度文心一言API,';

    // 拼接摘要
    $summary = $intro . '关键词:' . implode('、', $keywords) . '。' . $sentence;

    // 输出摘要
    echo $summary;
?>
Copy after login

The above code example will generate a blog summary, for example: This article comes from Baidu Wenxin Yiyan API, keywords: random, statement. This is a random statement.

Finally, we integrate the above code into our blog generation process to generate different blog summaries as needed. In this way, our blog will be more attractive and add some literary flavor.

Summary:

This article introduces a method of using PHP to connect to Baidu Wenxin Yiyan API to obtain random statements and generate blog summaries. Obtain random sentences by connecting to the API, and then generate blog summaries according to needs, which can increase the attractiveness and literary sense of the blog. I hope this article can help you improve the quality of your blog and reader experience.

The above is the detailed content of How to connect Baidu Wenxin Yiyan API with PHP to obtain random statements and generate blog summary. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template