该类可以请求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异步处理类相关推荐

  1. java8异步任务,Java8 - CompletableFuture 异步编程类

    其他异步编程方法 Java5中新增了Future接口,用来描述异步计算的结果.虽然 Future 以及相关使用方法提供了异步执行任务的能力,但是对于结果的获取却是很不方便,只能通过阻塞或者轮询的方式得 ...

  2. 怎样用uml类图生成java类_JAVA:面向对象编程的底层逻辑,深度剖析

    什么是面向对象 在目前的软件开发领域有两种主流的开发方法,分别是结构化开发方法和面向对象开发方法.早期的编程语言如C.Basic.Pascal等都是结构化编程语言,随着软件开发技术的逐渐发展,人们发现 ...

  3. python异步编程视频_asyncio异步编程【含视频教程】

    Python Python开发 Python语言 asyncio异步编程[含视频教程] 不知道你是否发现,身边聊异步的人越来越多了,比如:FastAPI.Tornado.Sanic.Django 3. ...

  4. 浅谈.Net异步编程的前世今生----异步函数篇(完结)

    前言 上一篇我们着重讲解了TPL任务并行库,可以看出TPL已经很符合现代API的特性:简洁易用.但它的不足之处在于,使用者难以理解程序的实际执行顺序. 为了解决这些问题,在C# 5.0中,引入了新的语 ...

  5. 【转】1.6异步编程:IAsyncResult异步编程模型 (APM)

    传送门:异步编程系列目录-- 大部分开发人员,在开发多线程应用程序时,都是使用ThreadPool的QueueUserWorkItem方法来发起一次简单的异步操作.然而,这个技术存在许多限制.最大的问 ...

  6. kafka实现异步发送_Kafka Producer 异步发送消息居然也会阻塞?

    Kafka 一直以来都以高吞吐量的特性而家喻户晓,就在上周,在一个性能监控项目中,需要使用到 Kafka 传输海量消息,在这过程中遇到了一个 Kafka Producer 异步发送消息会被阻塞的问题, ...

  7. springboot异步和切面_Spring异步编程 你的@Async就真的异步吗?异步历险奇遇记

    引言有点长 前端的宝宝会用ajax,用异步编程到快乐的不行~ 我们java也有异步,用起来比他们还快乐~ 我们biaji一个注(gǒupí)解(gāoyào),也是快乐风男... 且看下面的栗子: 注 ...

  8. springboot异步和切面_Spring异步编程 | 你的@Async就真的异步吗 ☞ 异步历险奇遇记...

    引言有点长 前端的宝宝会用ajax,用异步编程到快乐的不行~ 我们java也有异步,用起来比他们还快乐~ 我们bia~ji~一个注(gǒupí)解(gāoyào),也是快乐风男... 且看下面的栗子: ...

  9. springboot异步和切面_Spring异步编程 | 你的@Async就真的异步吗?异步历险奇遇记

    Spring异步编程 | 你的@Async就真的异步吗?异步历险奇遇记 点击上方"java进阶架构师",选择右上角"置顶公众号" 20大进阶架构专题每日送达 引 ...

  10. 异步编程-线程实现异步编程

    异步编程-线程实现异步编程 使用线程实现异步 第一种方式 第二种方式 问题 在日常开发中我们经常会遇到这样的情况,即需要异步地处理一些事情,而不需要知道异步任务的结果.比如在调用线程里面异步打日志,为 ...

最新文章

  1. Interactive Mathematics Study
  2. 一个权限树的设计与实现
  3. linux c 线程属性 pthread_attr_t 简介
  4. 利用php比较精确的统计在线人数的办法
  5. 【每日SQL打卡】​​​​​​​​​​​​​​​DAY 20丨查询球队积分【难度中等】​
  6. latex中怎么设置每一行文字都对齐_排版系列教程 | LaTeX,为学术论文排版而生【浮动体篇】...
  7. plsql提示列快捷键_20种VSCode快捷键清单,助你更快编码
  8. 复制mysql数据目录后无法启动的问题
  9. mysql:分组中某字段最大值的查询结果
  10. (四十七) 蓝牙自拍杆原理学习
  11. usb万能驱动win7_win10改win7教程
  12. 连接SQLserver数据库发生错误,提示用户sa登录失败解决方法(亲试有用)
  13. 国潮复兴——从红旗H9看HMI设计中的东方美学
  14. 著名企业求职面试指南
  15. 刘鹏教授在淮安市应急管理局作报告
  16. 最小二乘法和主成分分析的比较 matlab  儿子的papa
  17. hdu 3995 Perfect Faceless Void
  18. 信奥中的数学:排列组合
  19. 【MySQL从入门到精通】【高级篇】(一)字符集的修改与底层原理
  20. eBPF 是用来干什么的?

热门文章

  1. html文本域 高度自适应,textarea高度自适应,textarea随着内容高度变化
  2. linux源码文件名,Linux中文件名解析处理源码分析
  3. matlab线性拉伸函数,采用线性变换对图像的每一个像素灰度作线性拉伸-Read.PPT
  4. Java 设计模式 Day3 之面向抽象原则:什么是面向抽象编程?面向抽象编程如何应用?
  5. ZOJ 3804 YY's Minions (简单模拟)
  6. go为什么比php性能好,刚学 GO,撸了个支付宝发券的程序,为什么性能还比不上 PHP ?...
  7. 计算机辅助审计的特点是,浅谈新环境下计算机辅助审计的特点和应用_1
  8. excel单元格斜线_怎么在excel中画斜线?怎么在excel表格中画斜线?
  9. linux系统proc目录,快速了解Linux系统下的proc目录
  10. 修改蓝牙耳机按键映射_喜欢玩游戏的不要错过了,五款高性能游戏蓝牙耳机推荐...