Php asynchronous: Using fsockopen curl in php to implement functions similar to asynchronous processing, fsockopencurl

Source: Internet
Author: User

Php asynchronous: Using fsockopen curl in php to implement functions similar to asynchronous processing, fsockopencurl

From the mainstream perspective, PHP is a process-oriented language. The biggest drawback of PHP is that it cannot implement multi-threaded management. The execution of its programs is from start to end. It is impossible to have branches because it is implemented along the logic, this is one of the reasons to limit the development of php to more advanced languages in mainstream programming languages.

In PHP, we sometimes want to execute another operation while executing an operation. For example, when a user grabs a ticket, you do not want the user to queue up to connect to the database for query, judgment, and insertion, and then return the user results. In fact, we don't need to wait for a long time for the user. After the user submits the ticket, he can directly tell him that he has successfully obtained the ticket. As for various operations, just hand it over to the background for processing. Of course, in this case, we now use the message list to process the request submitted by each user in a message queue, telling the user that the request has been handled. After the user closes the page happily, in fact, the backend still extracts requests from the Message Queue one by one for operations. In this article, we use a different method to perform operations in the background without waiting for users.

First, we need to create a request entry:

<? The data submitted by php is submitted to the backend to notify the user that it has been done.

Second, we need a background processing program. Whether the user is online does not affect its operation:

<? Phpignore_user_abort (true); set_time_limit (0); Data Processing

The question is, in the first code section, how can I "Submit to the background "? We implement this function through a non-blocking request. That is, to create a url that can be accessed, run the second program on this url, request this url through a request, and then activate the second program to run automatically.

Next, let's look at the Code:

// Function _ sock ($ url) {$ host = parse_url ($ url, PHP_URL_HOST); $ port = parse_url ($ url, PHP_URL_PORT ); $ port = $ port? $ Port: 80; $ scheme = parse_url ($ url, PHP_URL_SCHEME); $ path = parse_url ($ url, PHP_URL_PATH); $ query = parse_url ($ url, PHP_URL_QUERY ); if ($ query) $ path. = '? '. $ Query; if ($ scheme = 'https') {$ host = 'ssl ://'. $ host ;}$ fp = fsockopen ($ host, $ port, $ error_code, $ error_msg, 1); if (! $ Fp) {return array ('error _ Code' => $ error_code, 'error _ msg '=> $ error_msg);} else {stream_set_blocking ($ fp, true ); // enable the non-blocking mode stream_set_timeout ($ fp, 1) in the manual; // set the timeout value $ header = "GET $ path HTTP/1.1 \ r \ n "; $ header. = "Host: $ host \ r \ n"; $ header. = "Connection: close \ r \ n"; // disable fwrite ($ fp, $ header) for persistent connections; usleep (1000 ); // This sentence is also critical. Without this delay, the fclose ($ fp) operation may fail on the nginx server ); return array ('error _ Code' => 0 );}}

We have created a function based on fsockopen, which uses fsockopen to access the url. However, when accessing the function, it does not require obtaining the content displayed by the url, but only sends an access request, close the access immediately after the request arrives. The advantage of this is that you no longer need to wait for the accessed url to return reliable information, saving time. The execution time of this Code is between 0.1-0.2 seconds. For common visitors, almost imperceptible. Therefore, you only need to call this function and the corresponding url. However, the data transmission section is not provided here. To transmit data, you only need to add the post content to the $ header.

In addition to fsockopen, curl can also achieve this effect. Some hosts do not support fsockopen, so we can use curl.

function _curl($url) { $ch = curl_init(); curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); curl_setopt($ch,CURLOPT_TIMEOUT,1); $result = curl_exec($ch); curl_close($ch); return $result;}

The key of this Code is to provide a Timeout, which is only one second. That is to say, curl will close the access after 1 second regardless of whether or not it receives the returned content, therefore, the execution data of this function is between 1.0-1.1 seconds. However, for users, if an application needs to process data, the wait time in one second is almost ignored. If you want to use a simpler and easier-to-understand code, you can select curl.

The above php asynchronous: Using fsockopen curl in php to implement a function similar to asynchronous processing is a small part of the Content shared to everyone, I hope to give you a reference, we also hope that you can support the customer's home.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.