PHP fsockopen Asynchronous Processing Instance Program

Source: Internet
Author: User
Tags chr strlen

Example

test.php

The code is as follows Copy Code

<?php


$domain = "localhost";
$url = '/platform_php_sdk/test4.php ';
$header = "POST $url http/1.0\r\n";
$header. = "content-type:application/x-www-form-urlencoded\r\n";

$par = "email=zhangzenglun@163.com";

$header. = "Content-length:". strlen ($par). \r\n\r\n ";
$fp = @fsockopen ($domain, $errno, $errstr, 30);
Fputs ($FP, $header. $par);
Fclose ($FP);

echo ' Send ok! ';

?>

test4.php

The code is as follows Copy Code

<?php

Set_time_limit (0);
Ignore_user_abort (TRUE);
$i = 0;
while ($i + + < 50) {
File_put_contents ($i. '. php ', $_request[' email '. $i);
Sleep (3);
}

?>

Supplement an asynchronous processing class

This class can request HTTP and HTTPS protocols, and can handle 301, 302 redirects, and gzip compression.
The code is as follows
asynHandle.class.php:

The code is as follows Copy Code

<?php




Class Asynhandle {


public $url = '; The full request URL passed in, including "http://" or "https://"


Public $cookie = Array (); An array of incoming cookies, which must be a pair of key values


Public $post = Array (); The incoming post array must be a key-value pair


Public $timeout = 30; Timeout number of seconds


public $result = '; The data that was obtained





Private $gzip = true; Whether to turn on gzip compression


Private $FOP = NULL; Fsockopen Resource Handle


Private $host = '; Host


Private $port = '; Port


Private $referer = '; Forged the antecedents


Private $requestUri = '; Actual request URI


Private $header = '; Header information





Private $block = 1; Network flow status. 1 is blocked, 0 is non-blocking


Private $limit = 128; The maximum number of bytes read





Constructors


Public Function __construct () {


Ignore_user_abort (TRUE);//Ignore user interrupts. If the client disconnects, it will not cause the script abort


Set_time_limit (0)//Cancel script execution delay limit


}


Resolving URLs and creating resource handles


Private Function Analyzeurl () {


if ($this-&gt;url = = ") {return false;}


$url _array = Parse_url ($this-&gt;url);


!isset ($url _array[' host ') &amp;&amp; $url _array[' host '] = ';


!isset ($url _array[' path ']) &amp;&amp; $url _array[' path ' = ';


!isset ($url _array[' query ') &amp;&amp; $url _array[' query '] = ';


!isset ($url _array[' Port ') &amp;&amp; $url _array[' port ' = 80;





$this-&gt;host = $url _array[' host '];


$this-&gt;port = $url _array[' Port '];


$this-&gt;referer = $url _array[' scheme '. ':/ /'. $this-&gt;host. ' /';


$this-&gt;requesturi = $url _array[' path '?


$url _array[' path ']. ($url _array[' query ')? '?'. $url _array[' query ']: '/';





Switch ($url _array[' scheme ']) {


Case ' HTTPS ':


$this-&gt;fop = fsockopen (' ssl://'. $this-&gt;host, 443, $errno, $errstr, $this-&gt;timeout);


Break


Default


$this-&gt;fop = Fsockopen ($this-&gt;host, $this-&gt;port, $errno, $errstr, $this-&gt;timeout);


Break


}





if (! $this-&gt;fop) {


$this-&gt;result = "$errstr ($errno) &lt;br/&gt;\n";


return false;


}


return true;


}//analyzeurl End





Header for assembling HTTP


Private Function Assheader () {


$method = Empty ($this-&gt;post)? ' Get ': ' POST ';


$gzip = $this-&gt;gzip? ' gzip, ': ';





Cookie data


if (!empty ($htis-&gt;cookie)) {


$htis-&gt;cookie = Http_build_cookie ($htis-&gt;cookie);


}





Post data


if (!empty ($this-&gt;post)) {


$this-&gt;post = http_build_query ($this-&gt;post);


}





$header = "$method $this-&gt;requesturi http/1.0\r\n";


$header. = "Accept: */*\r\n";


$header. = "Referer: $this-&gt;referer\r\n";


$header. = "accept-language:zh-cn\r\n";


if (!empty ($this-&gt;post)) {


$header. = "content-type:application/x-www-form-urlencoded\r\n";


}


$header. = "User-agent: $_server[http_user_agent]\r\n";


$header. = "Host: $this-&gt;host\r\n";


if (!empty ($this-&gt;post)) {


$header. = ' content-length: '. strlen ($this-&gt;post). " \ r \ n ";


}


$header. = "connection:close\r\n";


$header. = "Accept-encoding: {$gzip}deflate\r\n";


$header. = "Cookie: $this-&gt;cookie\r\n\r\n";


$header. = $this-&gt;post;


$this-&gt;header = $header;


}//assheader End





Return status detection, 301, 302 redirect Processing


Private Function Checkrecvheader ($header) {


if ($header, ' strstr ') | | strstr ($header, ' 302 ')) {//redirect processing


Preg_match ("/location: (. *?) $/im ", $header, $match);


$url = Trim ($match [1]);


Preg_match ("/set-cookie: (. *?) $/im ", $header, $match);


$cookie = (Empty ($match))? ': $match [1];





$obj = new Asynhandle ();


$result = $obj-&gt;get ($url, $cookie, $this-&gt;post);


$this-&gt;result = $result;


return $result;


}elseif (!strstr ($header, ' 200 ')) {


Domain name or URL not found


return false;


}else return 200;


}//checkrecvheader End





Gzip Decompression


Private Function Gzdecode ($data) {


$flags = Ord (substr ($data, 3, 1));


$headerlen = 10;


$extralen = 0;


$filenamelen = 0;


if ($flags &amp; 4) {


$extralen = Unpack (' V ', substr ($data, 10, 2));


$extralen = $extralen [1];


$headerlen + + 2 + $extralen;


}


if ($flags &amp; 8) $headerlen = Strpos ($data, Chr (0), $headerlen) + 1;


if ($flags &amp;) $headerlen = Strpos ($data, Chr (0), $headerlen) + 1;


if ($flags &amp; 2) $headerlen + = 2;


$unpacked = @gzinflate (substr ($data, $headerlen));


if ($unpacked = = FALSE) $unpacked = $data;


return $unpacked;


}//gzdecode End





Request function, request only, do not return


Public Function Request ($url, $cookie =array (), $post =array (), $timeout =3) {


$this-&gt;url = $url;


$this-&gt;cookie = $cookie;


$this-&gt;post = $post;


$this-&gt;timeout = $timeout;





if (! $this-&gt;analyzeurl ()) {


return $this-&gt;result;


}


$this-&gt;assheader ();





Stream_set_blocking ($this-&gt;fop, 0)//non-blocking, no waiting


Fwrite ($this-&gt;fop, $this-&gt;header);


Fclose ($this-&gt;fop);


return true;


}//request End





Gets the function, requests and returns


Public function Get ($url, $cookie =array (), $post =array (), $timeout =30) {


$this-&gt;url = $url;


$this-&gt;cookie = $cookie;


$this-&gt;post = $post;


$this-&gt;timeout = $timeout;





if (! $this-&gt;analyzeurl ()) {


return $this-&gt;result;


}


$this-&gt;assheader ();





Stream_set_blocking ($this-&gt;fop, $this-&gt;block);


Stream_set_timeout ($this-&gt;fop, $this-&gt;timeout);


Fwrite ($this-&gt;fop, $this-&gt;header);


$status = Stream_get_meta_data ($this-&gt;fop);





if (! $status [' timed_out ']) {


$h = ';


while (!feof ($this-&gt;fop)) {


if ($header = @fgets ($this-&gt;fop)) &amp;&amp; ($header = = "\ r \ n" | | $header = = "\ n")) {


Break


}


$h. = $header;


}





$checkHttp = $this-&gt;checkrecvheader ($h);


if ($checkHttp!=200) {return $checkHttp;}





$stop = false;


$return = ';


$this-&gt;gzip = false;


if (Strstr ($h, ' gzip ')) $this-&gt;gzip = true;


while (!) ( $stop &amp;&amp; $status [' timed_out '] &amp;&amp; feof ($this-&gt;fop))) {


if ($status [' timed_out ']) return false;


$data = Fread ($this-&gt;fop, ($this-&gt;limit = 0 | | $this-&gt;limit &gt; 128? 128: $this-&gt;limit));


if ($data = = ") {//Some servers are not allowed to be judged by themselves Foef


Break


}


$return. = $data;


if ($this-&gt;limit) {


$this-&gt;limit-= strlen ($data);


$stop = $this-&gt;limit &lt;= 0;


}





}


@fclose ($this-&gt;fop);


$this-&gt;result = $this-&gt;gzip? $this-&gt;gzdecode ($return): $return;


return $this-&gt;result;


}else{


return false;


}


}//get End


}

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.