Home > Article > Backend Development > PHP connects to Baidu Wenxin Yiyan API to obtain the number control method of specific types of sentences
PHP connects to Baidu Wenxin Yiyan API to obtain the number control method of a specific type of sentence
Introduction
Baidu Wenxin Yiyan is an API that provides a sentence service, which can provide various types of sentences, such as inspirational, philosophical, love, etc. This article will introduce how to use PHP to connect to Baidu Wenxin Yiyan API and control the number of sentences according to needs.
Steps
Generate signature
Before sending a request to Baidu Wenxin Yiyan API, you need to generate a signature to ensure data security. The signature is generated by encrypting the request parameters according to certain rules.
function generateSignature($params, $appSecret) { ksort($params); $signStr = ''; foreach ($params as $key => $value) { $signStr .= $key . $value; } return md5($signStr . $appSecret); }
Building request parameters
When building request parameters, you need to pass in the following parameters:
$params = array( 'appKey' => $appKey, 'type' => 'philosophy', 'count' => 10, 'charset' => 'UTF-8', ); $params['sign'] = generateSignature($params, $appSecret);
Send a request
Using PHP's cURL library can easily send HTTP requests and obtain the return results of the API.
$url = 'https://api.xiaokyo.com/wxyi/api/v1.0/sentence'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); curl_close($ch);
In this example, we use cURL's POST method to send the request, and URL-encode the request parameters before sending. Set CURLOPT_RETURNTRANSFER to true to save the return result of the API to the $response variable.
Parse the return results
After receiving the return results from the API, you can parse the return results and get the set of sentences you need.
$result = json_decode($response, true); if ($result['status'] == 0) { $sentences = $result['data']; foreach ($sentences as $sentence) { echo $sentence['content'] . " "; } } else { echo '请求错误:' . $result['status'] . ' - ' . $result['msg'] . " "; }
First, we use the json_decode function to convert the JSON format string returned by the API into an associative array. Then, we determine whether the request was successful by checking the status field of the returned result. If successful, you can get the content of each sentence by looping through the sentences array.
Summary
Through the above steps, we can use PHP to connect to Baidu Wenxin Yiyan API and obtain the number of specific types of sentences by controlling the count parameter. By parsing the returned results, we can operate and display the obtained sentences.
I hope this article can help PHP developers who are developing API interfaces. If you have any questions or need further information, please consult the Baidu Wenxin Yiyan API documentation.
The above is the detailed content of PHP connects to Baidu Wenxin Yiyan API to obtain the number control method of specific types of sentences. For more information, please follow other related articles on the PHP Chinese website!