Home  >  Article  >  Backend Development  >  PHP code implements scheduled task scheduling and execution of Baidu Wenxinyiyan API interface

PHP code implements scheduled task scheduling and execution of Baidu Wenxinyiyan API interface

WBOY
WBOYOriginal
2023-08-27 10:28:55669browse

PHP code implements scheduled task scheduling and execution of Baidu Wenxinyiyan API interface

PHP code implements scheduled task scheduling and execution of Baidu Wenxin Yiyan API interface

Hitokoto is a phrase or sentence that is widely circulated on the Internet Community. The Baidu Wenxin Yiyan API interface provides the function of randomly obtaining a Yiyan. You can obtain a random Wenxin Yiyan by calling the API interface and use it in your own projects. This article will introduce how to use PHP scheduled task scheduling to obtain and display a message.

A scheduled task is a task that can be automatically executed at a preset time interval or time point. In PHP, we can use crontab or timer to implement scheduled task scheduling.

First, we need to install a third-party library to send HTTP requests. For example, we can use the Guzzle HTTP library, installed through Composer:

composer require guzzlehttp/guzzle

Subsequently, we can write a PHP script to obtain and display a word. The specific code is as follows:

<?php
require 'vendor/autoload.php';

use GuzzleHttpClient;
use GuzzleHttpExceptionGuzzleException;

try {
    $client = new Client();
    $response = $client->request('GET', 'https://v1.hitokoto.cn');
    $data = json_decode($response->getBody()->getContents(), true);

    echo "一言内容:".$data['hitokoto']."
";
    echo "一言出处:".$data['from']."
";
} catch (GuzzleException $e) {
    echo "请求API接口失败。错误信息:" . $e->getMessage();
}
?>

The above code uses the Guzzle HTTP library to send a GET request and obtain the data returned by the API interface. We will display the content and source of the sentence we obtained.

Next, we can use crontab to set scheduled task scheduling.

crontab -e

Then, add the following line of code to the opened file:

*/5 * * * * /usr/bin/php /path/to/your/script.php > /dev/null 2>&1

The above code means that the PHP script is executed every 5 minutes, where/usr/bin/php is the path to the PHP interpreter, /path/to/your/script.php is the path to your PHP script file. > /dev/null 2>&1 means redirecting output to a null device to avoid unnecessary output.

Save the file and exit. crontab will automatically load and execute your scheduled tasks.

So far, we have completed the settings of regularly obtaining and displaying the code and scheduled task scheduling. Now, every 5 minutes, the scheduled task will call our PHP script, obtain a word of data and display it in the terminal.

Summary:
This article introduces how to use PHP to write scheduled task scheduling code, and how to use Baidu Wenxin Yiyan API interface to obtain Yiyan data and display it in your own project. Hope this article helps you!

The above is the detailed content of PHP code implements scheduled task scheduling and execution of Baidu Wenxinyiyan API interface. 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