日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php 异步处理类,php异步处理类

發布時間:2025/3/8 php 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php 异步处理类,php异步处理类 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

該類可以請求HTTP和HTTPS協議,還可以處理301、302重定向以及GZIP壓縮等。

[PHP]代碼

//使用方法

require('asynHandle.class.php');

$obj = new asynHandle();

$result = $obj->Request('http://baidu.com');

$result2 = $obj->Get('https://mail.google.com/');

echo "{$result}
";

echo "{$result2}
";

asynHandle.class.php?~?7KB????下載(10)

/*

* 異步處理類

* @author lkk/lianq.net

* @create on 10:05 2012-7-30

* @example:

* $obj = new asynHandle();

* $obj->Request('http://google.com');

* $obj->Get('http://google.com');

*/

class asynHandle {

public $url = ''; //傳入的完整請求url,包括"http://"或"https://"

public $cookie = array(); //傳入的cookie數組,須是鍵值對

public $post = array(); //傳入的post數組,須是鍵值對

public $timeout = 30; //超時秒數

public $result = ''; //獲取到的數據

private $gzip = true; //是否開啟gzip壓縮

private $fop = NULL; //fsockopen資源句柄

private $host = ''; //主機

private $port = ''; //端口

private $referer = ''; //偽造來路

private $requestUri = ''; //實際請求uri

private $header = ''; //頭信息

private $block = 1; //網絡流狀態.1為阻塞,0為非阻塞

private $limit = 128; //讀取的最大字節數

//構造函數

public function __construct(){

ignore_user_abort(TRUE);//忽略用戶中斷.如果客戶端斷開連接,不會引起腳本abort

//set_time_limit(0);//取消腳本執行延時上限

}

//解析URL并創建資源句柄

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)
\n";

return false;

}

return true;

}//analyzeUrl end

//拼裝HTTP的header

private function assHeader(){

$method = empty($this->post) ? 'GET' : 'POST';

$gzip = $this->gzip ? 'gzip, ' : '';

//cookie數據

if(!empty($htis->cookie)){

$htis->cookie = http_build_cookie($htis->cookie);

}

//post數據

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

//返回狀態檢測,301、302重定向處理

private function checkRecvHeader($header){

if(strstr($header,' 301 ') || strstr($header,' 302 ')){//重定向處理

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 ')){

//找不到域名或網址

return false;

}else return 200;

}//checkRecvHeader end

//gzip解壓

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 & 16) $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

//請求函數,只請求,不返回

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);//非阻塞,無須等待

fwrite($this->fop, $this->header);

fclose($this->fop);

return true;

}//Request end

//獲取函數,請求并返回

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 == ''){//有些服務器不行,須自行判斷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

}

版權申明:本站文章部分自網絡,如有侵權,請聯系:west999com@outlook.com

特別注意:本站所有轉載文章言論不代表本站觀點!

本站所提供的圖片等素材,版權歸原作者所有,如需使用,請與原作者聯系。

總結

以上是生活随笔為你收集整理的php 异步处理类,php异步处理类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。