<?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->url = = ") {return false;}
$url _array = Parse_url ($this->url);
!isset ($url _array[' host ') && $url _array[' host '] = ';
!isset ($url _array[' path ']) && $url _array[' path ' = ';
!isset ($url _array[' query ') && $url _array[' query '] = ';
!isset ($url _array[' Port ') && $url _array[' port ' = 80;
$this->host = $url _array[' host '];
$this->port = $url _array[' Port '];
$this->referer = $url _array[' scheme '. ':/ /'. $this->host. ' /';
$this->requesturi = $url _array[' path '?
$url _array[' path ']. ($url _array[' query ')? '?'. $url _array[' query ']: '/';
Switch ($url _array[' scheme ']) {
Case ' HTTPS ':
$this->fop = fsockopen (' ssl://'. $this->host, 443, $errno, $errstr, $this->timeout);
Break
Default
$this->fop = Fsockopen ($this->host, $this->port, $errno, $errstr, $this->timeout);
Break
}
if (! $this->fop) {
$this->result = "$errstr ($errno) <br/>\n";
return false;
}
return true;
}//analyzeurl End
Header for assembling HTTP
Private Function Assheader () {
$method = Empty ($this->post)? ' Get ': ' POST ';
$gzip = $this->gzip? ' gzip, ': ';
Cookie data
if (!empty ($htis->cookie)) {
$htis->cookie = Http_build_cookie ($htis->cookie);
}
Post data
if (!empty ($this->post)) {
$this->post = http_build_query ($this->post);
}
$header = "$method $this->requesturi http/1.0\r\n";
$header. = "Accept: */*\r\n";
$header. = "Referer: $this->referer\r\n";
$header. = "accept-language:zh-cn\r\n";
if (!empty ($this->post)) {
$header. = "content-type:application/x-www-form-urlencoded\r\n";
}
$header. = "User-agent: $_server[http_user_agent]\r\n";
$header. = "Host: $this->host\r\n";
if (!empty ($this->post)) {
$header. = ' content-length: '. strlen ($this->post). " \ r \ n ";
}
$header. = "connection:close\r\n";
$header. = "Accept-encoding: {$gzip}deflate\r\n";
$header. = "Cookie: $this->cookie\r\n\r\n";
$header. = $this->post;
$this->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->get ($url, $cookie, $this->post);
$this->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 & 4) {
$extralen = Unpack (' V ', substr ($data, 10, 2));
$extralen = $extralen [1];
$headerlen + + 2 + $extralen;
}
if ($flags & 8) $headerlen = Strpos ($data, Chr (0), $headerlen) + 1;
if ($flags &) $headerlen = Strpos ($data, Chr (0), $headerlen) + 1;
if ($flags & 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->url = $url;
$this->cookie = $cookie;
$this->post = $post;
$this->timeout = $timeout;
if (! $this->analyzeurl ()) {
return $this->result;
}
$this->assheader ();
Stream_set_blocking ($this->fop, 0)//non-blocking, no waiting
Fwrite ($this->fop, $this->header);
Fclose ($this->fop);
return true;
}//request End
Gets the function, requests and returns
Public function Get ($url, $cookie =array (), $post =array (), $timeout =30) {
$this->url = $url;
$this->cookie = $cookie;
$this->post = $post;
$this->timeout = $timeout;
if (! $this->analyzeurl ()) {
return $this->result;
}
$this->assheader ();
Stream_set_blocking ($this->fop, $this->block);
Stream_set_timeout ($this->fop, $this->timeout);
Fwrite ($this->fop, $this->header);
$status = Stream_get_meta_data ($this->fop);
if (! $status [' timed_out ']) {
$h = ';
while (!feof ($this->fop)) {
if ($header = @fgets ($this->fop)) && ($header = = "\ r \ n" | | $header = = "\ n")) {
Break
}
$h. = $header;
}
$checkHttp = $this->checkrecvheader ($h);
if ($checkHttp!=200) {return $checkHttp;}
$stop = false;
$return = ';
$this->gzip = false;
if (Strstr ($h, ' gzip ')) $this->gzip = true;
while (!) ( $stop && $status [' timed_out '] && feof ($this->fop))) {
if ($status [' timed_out ']) return false;
$data = Fread ($this->fop, ($this->limit = 0 | | $this->limit > 128? 128: $this->limit));
if ($data = = ") {//Some servers are not allowed to be judged by themselves Foef
Break
}
$return. = $data;
if ($this->limit) {
$this->limit-= strlen ($data);
$stop = $this->limit <= 0;
}
}
@fclose ($this->fop);
$this->result = $this->gzip? $this->gzdecode ($return): $return;
return $this->result;
}else{
return false;
}
}//get End
} |