Home >Backend Development >PHP Tutorial >Tutorial sharing on implementing Baidu Wenxinyiyan interface with PHP code

Tutorial sharing on implementing Baidu Wenxinyiyan interface with PHP code

WBOY
WBOYOriginal
2023-08-13 17:25:452014browse

Tutorial sharing on implementing Baidu Wenxinyiyan interface with PHP code

Tutorial sharing of PHP code to implement Baidu Wenxin Yiyan interface

Baidu Wenxin Yiyan is a very popular random sentence generation interface, which can be used on websites, Used in scenarios such as blogs to provide users with a concise and meaningful sentence. This article will introduce how to use PHP code to call Baidu Wenxin Yiyan interface to generate random sentences.

Step 1: Register a Baidu developer account
To use the Baidu Wenxin Yiyan interface, you first need to register a Baidu developer account and create an application. After the registration is completed, the corresponding API Key and Secret Key can be obtained from the Baidu Developer Platform, which will be used for subsequent interface calls.

Step 2: Send HTTP request
Use PHP code to send HTTP request and call Baidu Wenxin Yiyan interface. You can use the curl function to send a GET request to obtain the return data of the interface.

function getBaiduWenxinYiyan($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}

$url = "http://api.lwl12.com/hitokoto/main/get";
$response = getBaiduWenxinYiyan($url);

Step 3: Parse the data returned by the interface
Parse the JSON data returned by the interface and extract the required sentence content.

$result = json_decode($response, true);
if ($result && isset($result['source'])) {
    $sentence = $result['source'];
    echo "随机句子:".$sentence;
} else {
    echo "获取句子失败";
}

Step 4: Complete code example

function getBaiduWenxinYiyan($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}

$url = "http://api.lwl12.com/hitokoto/main/get";
$response = getBaiduWenxinYiyan($url);

$result = json_decode($response, true);
if ($result && isset($result['source'])) {
    $sentence = $result['source'];
    echo "随机句子:".$sentence;
} else {
    echo "获取句子失败";
}

Through the above code, you can realize the function of calling Baidu Wenxin Yiyan interface and obtaining random sentences. You can integrate the above code into your own website or blog, and call the code to display an interesting and thoughtful sentence to provide users with a better reading experience.

It should be noted that the Baidu Wenxin Yiyan interface is provided by a third party and is not an official interface. When using it, you should abide by the relevant usage specifications and agreements. In addition, if you have more advanced needs, you can also obtain more detailed sentence information by calling other interface parameters.

Summary
This article implements tutorial sharing for calling Baidu Wenxin Yiyan interface through PHP code. By registering a Baidu developer account, sending HTTP requests, and parsing the data returned by the interface, you can implement the random sentence generation function in your own website or blog. Hope this article is helpful to you.

The above is the detailed content of Tutorial sharing on implementing Baidu Wenxinyiyan interface with PHP 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