Home  >  Article  >  Backend Development  >  How to connect Baidu Wenxin Yiyan API with PHP to obtain random sentences and generate poems

How to connect Baidu Wenxin Yiyan API with PHP to obtain random sentences and generate poems

王林
王林Original
2023-08-14 13:06:151061browse

How to connect Baidu Wenxin Yiyan API with PHP to obtain random sentences and generate poems

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

With the rapid development of artificial intelligence technology and the improvement of natural language processing capabilities, We can use APIs to obtain some interesting data, such as Baidu Wenxin Yiyan API. This article introduces how to use PHP to connect to Baidu Wenxin Yiyan API to obtain random sentences, and display these sentences in the form of poetry.

First of all, we need to prepare:

  1. Register a Baidu developer account and obtain the API Key and Secret Key. For specific operation methods, please refer to the documentation on the Baidu Developer official website.
  2. Create a PHP file named poem_generator.php.

Next, let us write PHP code to realize the function of connecting to Baidu Wenxin Yiyan API and generating poetry.

<?php
// 定义API Key和Secret Key
define('API_KEY', 'your_api_key');
define('SECRET_KEY', 'your_secret_key');

// 定义API请求地址
define('API_URL', 'https://aip.baidubce.com/rpc/2.0/ai_custom/v1/text_cls/poem');

// 定义HTTP请求头
$headers = array(
    'Content-Type: application/json;charset=UTF-8',
);

// 构造API请求数据
$data = array(
    'model_id' => 'your_model_id',
    'text' => '',
);

// 获取随机语句
function getRandomSentence() {
    $curl = curl_init();
    $timeout = 30;
    $url = "https://v1.hitokoto.cn/";
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
    $result = curl_exec($curl);
    curl_close($curl);
    
    // 解析返回的JSON数据
    $data = json_decode($result, true);
    if(isset($data['hitokoto'])){
        return $data['hitokoto'];
    }
    return "";
}

// 生成诗歌
function generatePoem() {
    $sentence = getRandomSentence(); // 获取随机语句
    $data['text'] = $sentence;

    // 发送API请求
    $ch = curl_init(API_URL);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    $response = curl_exec($ch);
    curl_close($ch);

    // 解析API返回的结果
    $result = json_decode($response, true);
    if(isset($result['results']) && count($result['result']) > 0) {
        $poem = $result['result'][0]['poem']; // 获取诗歌内容
        echo $poem;
    } else {
        echo "生成诗歌失败";
    }
}

// 调用生成诗歌函数
generatePoem();
?>

In the above code, your_api_key and your_secret_key need to be replaced with your own API Key and Secret Key. At the same time, your_model_id also needs to be replaced with the model ID you created yourself.

In the code, we first define the API request address, request headers, and request data, and use the getRandomSentence() function to obtain random sentences. We then use the generatePoem() function to generate poetry from the results returned by the API request.

Save the above code as a poem_generator.php file and upload it to a server that supports PHP. Random poems can be generated by accessing the file.

Summary:

This article introduces the method of using PHP to connect to Baidu Wenxin Yiyan API to obtain random sentences and generate poems from these sentences. By calling the API interface, we can obtain interesting data and apply it to applications such as poetry generation, bringing more fun and creativity to our programs. Hope this article is helpful to you.

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