Home >Backend Development >PHP Tutorial >Analysis of the calling process of Baidu Wenxinyiyan interface in PHP
PHP Baidu Wenxin Yiyan interface calling process analysis
Background introduction:
Baidu Wenxin Yiyan is an API interface that provides random sentences. Used for website display of daily sentences and other functions. This article will introduce in detail the process of calling Baidu Wenxin Yiyan interface using PHP and provide code examples.
<?php // 引入CURL库 require 'path_to_curl_library.php';
<?php // 构建API请求 $url = 'https://api.wenxin.one/Api/?encode=json'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec($ch); curl_close($ch);
<?php // 解析API返回 $result = json_decode($response, true); if ($result && isset($result['content'])) { $content = $result['content']; echo $content; } else { echo '获取随机句子失败'; }
<?php // 引入CURL库 require 'path_to_curl_library.php'; // 构建API请求 $url = 'https://api.wenxin.one/Api/?encode=json'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec($ch); curl_close($ch); // 解析API返回 $result = json_decode($response, true); if ($result && isset($result['content'])) { $content = $result['content']; echo $content; } else { echo '获取随机句子失败'; }
Summary:
This article introduces the process of using PHP to call Baidu Wenxin Yiyan interface, and provides Complete code example. Through this interface, we can easily obtain random sentences and implement functions such as website display of daily sentences. Hope this article is helpful to everyone.
The above is the detailed content of Analysis of the calling process of Baidu Wenxinyiyan interface in PHP. For more information, please follow other related articles on the PHP Chinese website!