Table of Contents
分享语句
Home Backend Development PHP Tutorial 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

Aug 13, 2023 pm 02:09 PM
Baidu Wenxin Yiyan API PHP connects to Baidu Wenxin Yiyan API: php connection api Random sentence generation

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;
Copy after login

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 id="分享语句">分享语句</h1>
  <p>语句内容:<?php echo $quoteContent; ?></p>
  <p>作者:<?php echo $quoteAuthor; ?></p>
  
  <!-- 在这里添加社交媒体分享按钮,例如新浪微博、微信、QQ等 -->
</body>
</html>
Copy after login

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 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use Baidu Wenxin Yiyan API to implement a daily sentence in PHP development How to use Baidu Wenxin Yiyan API to implement a daily sentence in PHP development Aug 27, 2023 am 10:27 AM

How to use Baidu Wenxin Yiyan API to implement a daily sentence in PHP development. A concise and meaningful sentence can give people profound thinking and inspiration. In order to add some inspiration to your website, you can also use Baidu Wenxin Yiyan API to implement the function of one sentence per day. In this way, a different famous saying will be displayed every day, bringing more value and content to the website. First, we need to understand the basic usage of Baidu Wenxin Yiyan API. Baidu Wenxinyiyan API is a free API interface that provides a variety of types

Performance optimization techniques for developing and implementing Baidu Wenxinyiyan API interface using PHP Performance optimization techniques for developing and implementing Baidu Wenxinyiyan API interface using PHP Aug 26, 2023 pm 10:39 PM

Performance optimization techniques for using PHP to develop and implement Baidu Wenxin Yiyan API interface. With the popularity of the Internet, more and more developers use third-party API interfaces to obtain data to enrich their application content. Baidu Wenxin Yiyan API interface is a popular data interface. It can return a random inspirational, philosophical or warm sentence, which can be used to beautify the program interface, increase user experience, etc. However, when using the Baidu Wenxinyiyan API interface, we also face some performance considerations. API call speed

Implementation steps for connecting Baidu Wenxin Yiyan API with PHP to obtain a daily sentence Implementation steps for connecting Baidu Wenxin Yiyan API with PHP to obtain a daily sentence Aug 25, 2023 pm 08:28 PM

Implementation steps for connecting Baidu Wenxin Yiyan API with PHP to obtain daily sentences. Hitokoto is an open sentence interface that can obtain various types of sentences, such as animations, comics, novels, etc. In this article, we will introduce how to use PHP to connect to Baidu Wenxin Yiyan API to obtain and display a daily sentence. Step 1: Apply for API key First, we need to go to Baidu Open Cloud (https://cloud.baidu.com/) website to register an account. Then, in the console create

PHP code implements sensitive word filtering and replacement processing of Baidu Wenxinyiyan API interface PHP code implements sensitive word filtering and replacement processing of Baidu Wenxinyiyan API interface Aug 26, 2023 pm 05:06 PM

PHP code implements sensitive word filtering and replacement processing of Baidu Wenxin Yiyan API interface. In modern social networks and instant messaging tools, the filtering and replacement of sensitive words is a very important task. Such filtering can protect users from harmful information while also maintaining the health and order of the network environment. Baidu Wenxin Yiyan API interface provides a convenient and practical sensitive word filtering and replacement service, and PHP, as a scripting language widely used in web development, can be used to implement this function. Next, I will give you

PHP code implements request parameter encryption and decryption processing of Baidu Wenxinyiyan API interface PHP code implements request parameter encryption and decryption processing of Baidu Wenxinyiyan API interface Aug 16, 2023 pm 11:40 PM

The PHP code implements the request parameter encryption and decryption processing of Baidu Wenxin Yiyan API interface. Hitokoto is a service that provides access to random sentences. Baidu Wenxin Yiyan API is one of the interfaces that developers can call. In order to ensure data security, we can encrypt the request parameters and decrypt the response after receiving the response. The following is an example of PHP code implementing the request parameter encryption and decryption processing of Baidu Wenxinyiyan API interface: &lt;?phpfunction

How to implement the interface restrictions and frequency control of Baidu Wenxinyiyan API in PHP development? How to implement the interface restrictions and frequency control of Baidu Wenxinyiyan API in PHP development? Aug 25, 2023 pm 08:45 PM

How to implement the interface restrictions and frequency control of Baidu Wenxinyiyan API in PHP development? Overview: Baidu Wenxin Yiyan API is a very popular interface for obtaining various types of random sentences, including inspirational, emotional, famous quotes, etc. However, since this interface is accessed frequently on the Internet, in order to protect server resources and ensure the stability of the interface, developers need to limit and control the access frequency of the interface. This article will introduce how to use PHP development to implement interface restrictions and frequency control. Implementation steps: Step1:

PHP code implements response gzip compression and decompression of Baidu Wenxinyiyan API interface PHP code implements response gzip compression and decompression of Baidu Wenxinyiyan API interface Aug 14, 2023 pm 11:37 PM

PHP code implements the response gzip compression and decompression of Baidu Wenxin Yiyan API interface. Nowadays, network communication has become an indispensable part of people's daily life. In order to improve the efficiency of network transmission and save bandwidth resource consumption, many websites use gzip compression technology to compress data before transmitting it. Baidu Wenxin Yiyan API also provides an interface that supports gzip compression. This article will introduce how to use PHP code to implement gzip compression and decompression in response to Baidu Wenxin Yiyan API interface. first,

PHP code implements CORS cross-domain processing of response results of Baidu Wenxinyiyan API interface PHP code implements CORS cross-domain processing of response results of Baidu Wenxinyiyan API interface Aug 12, 2023 pm 12:09 PM

PHP code implements CORS cross-domain processing of response results of Baidu Wenxin Yiyan API interface. In web development, cross-origin resource sharing (Cross-OriginResourceSharing) is a common problem. When we request resources under one domain name from a web page under another domain name, if cross-domain processing is not performed, the browser will block the request by default. In actual development, we often need to call third-party interfaces in the front-end page to obtain data. This article will introduce how to use P

See all articles