Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the common ways of asynchronous execution based on PHP

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the common ways of asynchronous execution based on PHP, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

1. The client page requests the server using AJAX technology

Pros: the easiest and fastest is to embed an AJAX call in the HTML code returned to the client, or an img tag, and the src points to the time-consuming script to execute.

Disadvantages: generally speaking, Ajax should be triggered after onLoad, that is, after the user clicks on the page, it will be closed, so our background script will not be triggered.

With img tags, this approach cannot be called strictly asynchronous execution. The user's browser waits for a long time for the execution of the php script to complete, that is, the user's browser's status bar always shows that it is still on load.

Of course, there are other similar principles that can be used, such as script tags, and so on.

2.popen () function

This function opens a pipeline to a process that is generated by deriving the execution of a given command command. Opens a pipe to a process that is generated by deriving the execution of a given command command.

So you can call it, but ignore its output. The usage code is as follows:

The copy code is as follows:

Pclose (popen ("/ home/xinchen/backend.php &",'r'))

Advantages: avoids the disadvantages of the first method, and is also very fast.

Disadvantages: this method cannot request another WebService through the HTTP protocol and can only execute local script files. And can only be opened one-way, can not wear a large number of parameters to the called script. And if, when the traffic is high, a large number of processes will be generated. If you use external resources, you have to consider your own competition.

3.CURL extension

CURL is a powerful HTTP command-line tool that simulates HTTP requests such as POST/GET, and then gets and extracts data that is displayed on "standard output" (stdout). The code is as follows:

The copy code is as follows:

$ch = curl_init ()

$curl_opt = array (CURLOPT_URL, 'http://www.example.com/backend.php',

CURLOPT_RETURNTRANSFER, 1

CURLOPT_TIMEOUT, 1,)

Curl_setopt_array ($ch, $curl_opt)

Curl_exec ($ch)

Curl_close ($ch)

Cons: as described in your question, you need to set CUROPT_TIMEOUT to 1 (minimum 1, depressed) due to the use of CURL. That is, the client must wait at least 1 second.

4.fscokopen () function

Fsockopen supports socket programming, you can use fsockopen to implement socket programs such as sending mail, and so on. Using fcockopen, you need to manually splice out the header parts.

Examples of use are as follows:

The copy code is as follows:

$fp = fsockopen ("www.34ways.com", 80, $errno, $errstr, 30)

If (! $fp) {

Echo "$errstr ($errno)\ n"

} else {

$out = "GET / index.php / HTTP/1.1\ r\ n"

$out. = "Host: www.34ways.com\ r\ n"

$out. = "Connection: Close\ r\ n\ n"

Fwrite ($fp, $out)

/ * ignore the execution result

While (! feof ($fp)) {

Echo fgets ($fp, 128)

} * /

Fclose ($fp)

}

So to sum up, the fscokopen () function should meet your requirements. You can try it.

Thank you for reading this article carefully. I hope the article "what are the common ways of asynchronous execution based on PHP" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report