Home  >  Article  >  Backend Development  >  How to connect PHP to Baidu Wenxinyiyan interface?

How to connect PHP to Baidu Wenxinyiyan interface?

WBOY
WBOYOriginal
2023-08-13 21:27:281239browse

How to connect PHP to Baidu Wenxinyiyan interface?

How does PHP connect to Baidu Wenxinyiyan interface?

Baidu Wenxin Yiyan interface is an interface that provides a random return of Wenxin words. It is very simple to use. This article will introduce how PHP connects to Baidu Wenxin Yiyan interface and provide code examples.

First of all, we need to apply for access to Baidu Wenxin Yiyan interface. Create an application on Baidu Open Platform and obtain the corresponding API Key.

Next, we can use the cURL library in PHP to send HTTP requests and get the data returned by the interface. The following is a simple PHP function that can complete the call to Baidu Wenxin Yiyan interface:

function getBaiduWenxinYiyan($apiKey) {
    $url = 'http://api.lwl12.com/hitokoto/main/get?key=' . $apiKey;
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
    $result = curl_exec($ch);
    
    curl_close($ch);
    
    return $result;
}

In the function, we first construct the URL to access Baidu Wenxin Yiyan interface, and splice the API Key to The end of the URL. Then, use the cURL library to open a new cURL session and set the URL and options. Finally, execute the cURL session and get the returned results. Finally, the cURL session is closed and the results are returned.

Next, we can call this function to get Wenxinhua. For example:

$apiKey = 'your_api_key';
$result = getBaiduWenxinYiyan($apiKey);
echo $result;

In the code example, we replace the API Key with the API Key we applied for, and call the function to output the result to the browser.

Of course, we can also parse and process the returned data to extract the content we need to use. For example, you can convert the obtained Wenxin words into JSON format and extract the sentences and sources:

$apiKey = 'your_api_key';
$result = getBaiduWenxinYiyan($apiKey);
$data = json_decode($result, true);

if ($data && isset($data['hitokoto']) && isset($data['from'])) {
    $sentence = $data['hitokoto'];
    $source = $data['from'];
    echo "句子:{$sentence}<br>";
    echo "出处:{$source}<br>";
} else {
    echo "获取文心话失败";
}

The above code first performs JSON analysis on the returned results and stores the results in the $data variable. . Then, it determines whether the parsing result is valid, extracts the sentence and source, and outputs it to the browser. If the acquisition of Wenxin words fails, the corresponding error message will be output.

Through the above code examples, we can easily connect to Baidu Wenxin Yiyan interface in PHP and obtain a random Wenxin Yiyan. This is very useful for building personal websites, generating diaries, writing and other scenarios. Hope this article can help you.

The above is the detailed content of How to connect PHP to Baidu Wenxinyiyan interface?. 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