How to implement the asynchronous callback of Baidu Wenxin Yiyan interface in PHP development?
Hitokoto, as a short sentence service full of philosophy, emotion and randomness, is deeply loved by many developers and users. Baidu provides an interface that can obtain information through asynchronous callbacks. So, this article will introduce how to implement the asynchronous callback of Baidu Wenxin Yiyan interface in PHP development.
First, we need to register a Baidu developer account and create an application to obtain the API Key and Secret Key required to access the API. Next, we can start writing code.
First, create a file named hitokoto.php
and add the following code:
<?php $apiKey = '你的API Key'; $secretKey = '你的Secret Key'; $url = 'http://api.hitokoto.cn/?encode=json'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_HTTPHEADER, ['X-Bce-Signature-Key: ' . $apiKey . ':' . $secretKey]); $response = curl_exec($ch); if (curl_errno($ch)) { echo 'Error: ' . curl_error($ch); } else { echo $response; } curl_close($ch);
In the above code, we first define the API Key and Secret Key, And set the interface URL to be requested. Next, we use the curl_init()
function to initialize a CURL session and set some CURL options through the curl_setopt()
function. Among them, CURLOPT_URL
specifies the URL to be accessed, CURLOPT_RETURNTRANSFER
is set to 1 to return the requested return value in the form of a string, and CURLOPT_HEADER
is set to 0 to not return Response header information, CURLOPT_TIMEOUT
sets the timeout, CURLOPT_HTTPHEADER
sets the request header information, including API Key and Secret Key. Next, we send the request through the curl_exec()
function and check for errors through the curl_errno()
function. Finally, we close the CURL session using the curl_close()
function.
After saving the file, we can get the content of Yiyan by accessing the hitokoto.php
file.
The above is a way to use CURL. In addition, we can also use PHP's file_get_contents()
function to achieve this.
Modify the code of the hitokoto.php
file as follows:
<?php $apiKey = '你的API Key'; $secretKey = '你的Secret Key'; $url = 'http://api.hitokoto.cn/?encode=json'; $context = stream_context_create([ 'http' => [ 'method' => 'GET', 'header' => 'X-Bce-Signature-Key: ' . $apiKey . ':' . $secretKey, ], ]); $response = file_get_contents($url, false, $context); if ($response === false) { echo 'Error: ' . error_get_last()['message']; } else { echo $response; } ?>
In the above code, we first define the API Key and Secret Key, and set the interface URL to be requested. . Next, we create a context through the stream_context_create()
function, where 'http'
specifies the request method as GET, and sets the request header information, including API Key and Secret Key . Finally, we send the request through the file_get_contents()
function, and determine whether the request is successful by comparing whether the returned result is false
.
The above is a code example for implementing asynchronous callback of Baidu Wenxin Yiyan interface in PHP development. Through these methods, we can easily obtain the content of Yiyan and use it in our own applications. Hope this article is helpful to you!
The above is the detailed content of How to implement asynchronous callback of Baidu Wenxin Yiyan interface in PHP development?. For more information, please follow other related articles on the PHP Chinese website!