Home > Backend Development > PHP Tutorial > PHP code implements data formatting and conversion of Baidu Wenxinyiyan API interface

PHP code implements data formatting and conversion of Baidu Wenxinyiyan API interface

王林
Release: 2023-08-12 08:44:02
Original
1307 people have browsed it

PHP code implements data formatting and conversion of Baidu Wenxinyiyan API interface

PHP code implements data formatting and conversion of Baidu Wenxin Yiyan API interface

Foreword:
Baidu Wenxin Yiyan is a program that provides random sentences API interface, the returned data format is JSON. This article will introduce how to use PHP code to obtain data by calling Baidu Wenxin Yiyan API interface, and format and convert the returned JSON data.

1. Obtain data
First, we need to call Baidu Wenxin Yiyan API interface through HTTP request to obtain data. This process can be easily achieved by using PHP's cURL library. The following is a simple code example:

<?php

$url = 'https://api.ixiaowai.cn/gqapi/gqapi.php'; // 你的API接口地址

// 创建cURL资源
$ch = curl_init($url);

// 配置cURL选项
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// 发送请求并获取返回结果
$response = curl_exec($ch);

// 关闭cURL资源
curl_close($ch);

// 输出原始返回结果
echo $response;

?>
Copy after login

We can save the above code as a PHP file and run it in the browser to see the original JSON data returned by the API.

2. Parse JSON data
Next, we need to parse the returned JSON data and convert it into a PHP array or object to facilitate subsequent data processing. PHP provides the json_decode function to implement this function. The following is a sample code:

<?php

$url = 'https://api.ixiaowai.cn/gqapi/gqapi.php'; // 你的API接口地址

// 创建cURL资源
$ch = curl_init($url);

// 配置cURL选项
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// 发送请求并获取返回结果
$response = curl_exec($ch);

// 关闭cURL资源
curl_close($ch);

// 解析JSON数据
$data = json_decode($response);

// 输出解析后的数据
var_dump($data);

?>
Copy after login

Run the above code, you will see that the parsed data is output in the form of a PHP array.

3. Data formatting and conversion
Next, we can format and convert the parsed data to meet our needs. The following is a sample code that escapes the special characters in the returned sentence data and then outputs:

<?php

$url = 'https://api.ixiaowai.cn/gqapi/gqapi.php'; // 你的API接口地址

// 创建cURL资源
$ch = curl_init($url);

// 配置cURL选项
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

// 发送请求并获取返回结果
$response = curl_exec($ch);

// 关闭cURL资源
curl_close($ch);

// 解析JSON数据
$data = json_decode($response);

// 格式化与转换数据
$format_data = htmlspecialchars($data->data->content);

// 输出转换后的数据
echo $format_data;

?>
Copy after login

In the above code, we use the htmlspecialchars function to escape the special characters to ensure the output content Will not affect HTML rendering.

Conclusion:
Through the above code examples, we can use PHP code to call the Baidu Wenxin Yiyan API interface to obtain data, and format and convert the returned JSON data. You can further process and utilize this data according to your own needs, such as displaying it on a web page or storing it in a database. At the same time, you can also adjust and improve the above sample code according to the specific documentation of Baidu Wenxin Yiyan API to meet more needs.

The above is the detailed content of PHP code implements data formatting and conversion of Baidu Wenxinyiyan API interface. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template