Home  >  Article  >  Backend Development  >  How to connect Baidu Wenxin Yiyan API with PHP to obtain random sentences and generate social sharing links

How to connect Baidu Wenxin Yiyan API with PHP to obtain random sentences and generate social sharing links

PHPz
PHPzOriginal
2023-08-13 14:09:061658browse

How to connect Baidu Wenxin Yiyan API with PHP to obtain random sentences and generate social sharing links

How to connect PHP to Baidu Wenxin Yiyan API to obtain random sentences and generate social sharing links

In today's social media era, people are keen to share their moods , opinions and feelings. Quotations with concise and interesting sentences can often arouse the resonance and attention of others. Baidu Wenxin Yiyan API provides such a service. It can obtain random interesting sentences so that we can use them for social sharing link generation. Today, we will learn how to use PHP to connect to Baidu Wenxin Yiyan API and generate social sharing links from the obtained sentences.

First, we need to obtain the interface address and related parameters of Baidu Wenxin Yiyan API. The interface address of Baidu Wenxin Yiyan API is: https://api.vvhan.com/api/wenyanapi. Its parameters are: c is classification, type is number, the specific value and representative meaning are as follows:

  • 1: Ancient Chinese
  • 2: Synonyms
  • 3 : Inspirational
  • 4: Love words
  • 5: Lines
  • 6: Game

Next, we use PHP’s cURL library to send HTTP Request to get data. The following is a code example:

<?php

function getWenXinYiYan($category) {
  $apiUrl = "https://api.vvhan.com/api/wenyanapi";

  $curl = curl_init();
  curl_setopt($curl, CURLOPT_URL, $apiUrl."?c=".$category);
  curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  $response = curl_exec($curl);
  curl_close($curl);

  return $response;
}

$category = 3; // 励志语句

$quote = getWenXinYiYan($category);

// 处理获取到的语句
$quote = json_decode($quote, true);
$quoteContent = $quote['data']['contents'];
$quoteAuthor = $quote['data']['author'];

// 生成社交分享链接
$quoteUrl = "https://www.example.com/quote.php?c=".$category."&q=".urlencode($quoteContent)."&a=".urlencode($quoteAuthor);

echo "获取到的语句:" . $quoteContent . PHP_EOL;
echo "作者:" . $quoteAuthor . PHP_EOL;
echo "社交分享链接:" . $quoteUrl . PHP_EOL;

In the above code, we use the getWenXinYiYan function to send an HTTP request and return the API response. Then, we process the obtained statements and extract the statement content and author. Finally, we generate social sharing links by splicing URLs.

The format of the generated social sharing link is: https://www.example.com/quote.php?c=3&q=[statement content]&a=[author]. You can replace it with your own website domain name and page path.

In the quote.php page, you can display the statement content and author information according to the values ​​of parameters c, q and a, and use them for social sharing. The following is a code example of quote.php:

<?php

$category = $_GET['c'];
$quoteContent = $_GET['q'];
$quoteAuthor = $_GET['a'];

?>

<!DOCTYPE html>
<html>
<head>
  <title>语句分享</title>
</head>
<body>
  <h1>分享语句</h1>
  <p>语句内容:<?php echo $quoteContent; ?></p>
  <p>作者:<?php echo $quoteAuthor; ?></p>
  
  <!-- 在这里添加社交媒体分享按钮,例如新浪微博、微信、QQ等 -->
</body>
</html>

In the quote.php page, we obtain the passed parameter value through $_GET and display the statement content and author information. You can add social media sharing buttons according to your needs.

In this way, we use PHP to connect to Baidu Wenxin Yiyan API to obtain random statements, and implement the method of generating social sharing links. You can choose different sentence categories according to your preferences and website needs, and use the obtained sentences for social sharing.

I hope this article will be helpful to you, and I wish you success in using the Baidu Wenxin Yiyan API and the function of generating social sharing links!

The above is the detailed content of How to connect Baidu Wenxin Yiyan API with PHP to obtain random sentences and generate social sharing links. 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