PHP Asynchronous: Using Fsockopen curl in PHP to implement a functional approach similar to asynchronous processing _php skills

Source: Internet
Author: User
Tags curl message queue nginx server

PHP from the mainstream, is a process-oriented language, its biggest drawback is the inability to achieve multithreaded management, the implementation of its programs are from beginning to end, according to the logic all the way down, impossible to appear branches, which is to restrict PHP in the mainstream programming language to more advanced language development of one of the reasons.

In PHP we sometimes want to do some operation, while to perform another operation, to give a scene: When users Rob tickets, you do not want users to queue to connect the database to query, judge, insert, complete and then return the user results. In fact, we do not need users to wait so long time, the user submitted, directly to tell him that the ticket has been successful on it, as for a variety of operations, to the background to deal with the good. Of course, we now all use the message list to deal with each user submitted requests exist in a message queue, told the user has been done, the user happy to turn off the page, in fact, the background also in a message queue to take out the request to operate. Our article is a heterogeneous approach, the implementation of the operation in the background, without the user waiting.

First, we want to create a request entry:

<?php

submitted data

submitted to the background to

tell the user has been done

Secondly, we need a background handler, whether the user is online does not affect its operation:

<?php

Ignore_user_abort (true);
Set_time_limit (0);

Data processing over the

The question now is, how do you "submit to the background" in the first piece of code? We do this by means of a non-blocking request. That is, create a URL that can be accessed, run the second program at this URL, request the URL through a request, and activate the second program to run automatically.

Next we'll look at the code directly:

/Remote request (do not get content) function functions _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);/Open the Stream_set_timeout mode in the Manual ($FP, 1);/set Timeout $header = "Get $path http/1
  .1\r\n ";
  $header. = "Host: $host \ r \ n";
  $header. = "connection:close\r\n\r\n";//Long connection close fwrite ($fp, $header); Usleep (1000);
  This sentence is also the key, if not this delay, may be on the Nginx server can not perform successful fclose ($FP);
 Return Array (' Error_code ' => 0); }
}

We created a fsockopen function that uses Fsockopen to access the URL, but when accessed, it does not require a URL to display, but rather just an access request, which closes immediately after the request arrives. The advantage of this is that no longer waits for the URL to be accessed to return reliable information, saving time, this code execution time between 0.1-0.2 seconds, for ordinary visitors, almost imperceptible. Therefore, when you use it, you only need to call this function and the corresponding URL. However, there is no part of the data transmission, how to transfer data, in fact, only need to increase the post content in the $header.

In addition to Fsockopen,curl can actually achieve this effect, some hosts do not support Fsockopen, we can use curl to achieve.

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 to this code is to provide a timeout, only 1 seconds, that is, the curl makes a request, regardless of whether it receives the returned content, closes the access after 1 seconds, so the execution data for this function is between 1.0-1.1 seconds. But for users, if it is an application that requires data processing, the 1-second wait is almost ignored, if you want to use a simpler and easier to understand code, you can choose Curl to implement.

Above this PHP asynchronous: Use Fsockopen curl in PHP to implement similar asynchronous processing function method is small series to share the whole content of everybody, hope can give everybody a reference, also hope that we support cloud habitat community a lot.

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.