php通过curl实现bigpipe
BigPipe是facebook发明的一种页面加载技术。其实也不是什么新技术了,也有很多文章进行说明。但是在网上,具体讲如何使用php实现bigpipe的文章很少,并且有些文章还不很准确。bigpipe的核心思想是并行。作为后端也要并行数据处理,这样才能使bigpipe发挥最大的威力。
在实现bigpipe时有几点注意:
1.如果使用nginx作为web服务器,那么nginx可能会缓冲php的输出。即便是调用了flush方法,相应内容也会被nginx缓冲,而不会输出到浏览器。
2.某些浏览器也会有缓冲,如在接收的数据小于一定值的时候,不会对代码进行渲染。
以下是一个demo示例代码:
<?php
header("Transfer-Encoding","chunked");
ini_set("output_buffering",0);
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>php实现BigPipe</title>
<style type="text/css">
<!--
body {
font-size:12px;
text-align:left;
}
#head {
width:100%;
height:40px;
background:#8CAADE;
font-weight:bold;
color:#FFFFFF;
line-height:40px;
text-align:center;
font-size:16px;
}
#body {
margin:5px auto 5px auto;
width:800px;
border:solid 1px #8CAADE;
padding:10px;
line-height:30px;
}
#left {
float:left;
width:50%;
border:0px;
line-height:23px;
}
#right {
float:right;
width:50%;
border:0px;
line-height:23px;
}
#result {
width:800px;
height:30px;
text-align:center;
}
#bottom {
margin-top:5px;
width:800px;
height:30px;
text-align:center;
display:none;
}
#bottom a {
color:red;
text-decoration:none;
}
-->
</style>
<script>
function update(id, content) {
document.getElementById(id).innerHTML = content;
}
</script>
</head>
<?php
ob_flush();
flush();
?>
<body>
<div id="head">php实现BigPipe</div>
<div id="body">
<div>
本demo旨在演示php的并发处理在bigpipe中的应用。因此,在一些细节上实现的比较简单。但是,demo完全体现了bigpipe的并行数据处理和并行加载。<br/>
慢动作体现的是后端php串行处理时的效果。
</div>
<div id="cost_time"></div>
<?php
/*
* @purpose: 使用curl并行处理url
* @return: array 每个url获取的数据
* @param: $urls array url列表
* @param: $callback string 需要进行内容处理的回调函数。示例:func(array)
*/
function curl($urls = array(), $callback = '')
{
$response = array();
if (empty($urls)) {
return $response;
}
$chs = curl_multi_init();
$map = array();
foreach($urls as $url){
$cookie_file = "/temp/".md5($url).".txt";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 2);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_NOSIGNAL, true);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
curl_multi_add_handle($chs, $ch);
$map[strval($ch)] = $url;
}
do{
if (($status = curl_multi_exec($chs, $active)) != CURLM_CALL_MULTI_PERFORM) {
if ($status != CURLM_OK) { break; } //如果没有准备就绪,就再次调用curl_multi_exec
while ($done = curl_multi_info_read($chs)) {
$info = curl_getinfo($done["handle"]);
$error = curl_error($done["handle"]);
$result = curl_multi_getcontent($done["handle"]);
$url = $map[strval($done["handle"])];
$rtn = compact('info', 'error', 'result', 'url');
if (trim($callback)) {
$callback($rtn);
}
$response[$url] = $rtn;
curl_multi_remove_handle($chs, $done['handle']);
curl_close($done['handle']);
//如果仍然有未处理完毕的句柄,那么就select
if ($active > 0) {
curl_multi_select($chs, 0.5); //此处会导致阻塞大概0.5秒。
}
}
}
}
while($active); //还有句柄处理还在进行中
curl_multi_close($chs);
return $response;
}
//使用方法
function deal($data){
$url_info = parse_url($data["url"]);
$content_id = "content_".md5($data["url"]);
if ($data["error"] == "") {
$content = $url_info["host"]." 能正常打开";
echo "<script>update('$content_id', '$content')</script>";
} else {
$content = $url_info["host"]." 访问失败。具体错误原因:".$data["error"];
echo "<script>update('$content_id', '$content')</script>";
}
//因为浏览器有缓冲,即获取数据后,不会马上渲染。当获取数据大于一个值时开始渲染。
echo str_repeat(' ',1024);
ob_flush();
flush();
if (!empty($_GET["m"]) && $_GET["m"] == "slow") {
sleep(1);
}
}
$keyword = "一淘网";
$google_urls = array(
'http://www.google.com.br',//巴西
'http://www.google.ch',//瑞士
'http://www.google.nl',//荷兰
'http://www.google.com.au',//澳大利亚
'http://www.google.co.in',//印度
'http://www.google.ro',//罗马尼亚
'http://www.google.co.th',//泰国
'http://www.google.com.sa',//沙特阿拉伯
'http://www.google.co.jp',//日本
'http://www.google.com.my',//马来西亚
'http://www.google.ca',//加拿大
'http://www.google.com.tw',//中国台湾
);
$google_num = 10;//从google网址中取几个
$keys = array_rand($google_urls,$google_num);
$urls[] = "http://www.baidu.com/s?wd=".urlencode($keyword);
$urls[] = "http://www.etao.com";
for ( $i = 0; $i < $google_num; $i++) {
$urls[] = $google_urls[$keys[$i]]."/search?sclient=psy-ab&hl=en&site=&source=hp&q=".urlencode($keyword);
}
ob_start();
//生成占位div
foreach($urls as $url){
echo "<div class='item' id='content_".md5($url)."'>{$url} 检测中</div>";
}
//除了浏览器web服务器可能也会缓冲,如nginx的fastcgi_buffers。
echo str_repeat(' ',1024);
ob_end_flush();
ob_flush();
flush();
//开始获取搜索数据
$start = microtime(true);
curl($urls, "deal");
$cost_time = microtime(true) - $start;
echo "<script>update('cost_time', '耗时{$cost_time}秒 <a href=\'?m=slow\'>查看慢动作</a>')</script>";
?>
</div>
</body>
</html>
原创文章转载于: php实现bigpipe | 博学无忧
演示地址: http://demo.bo56.com/bigpipe/
本文固定链接: http://www.bo56.com/php%e5%ae%9e%e7%8e%b0bigpipe/ | 博学无忧
注意:使用BigPipe时,前端尽量不要使用nginx作为前端web服务器。因为他的fast-cgi的buffer无法关闭。也不要启用gzip等压缩。
php通过curl实现bigpipe的更多相关文章
- BigPipe学习研究
BigPipe学习研究 from: http://www.searchtb.com/2011/04/an-introduction-to-bigpipe.html 1. 技术背景 FaceBook ...
- [转载]BigPipe技术
1. 技术背景 FaceBook页面加载技术 试想这样一个场景,一个经常访问的网站,每次打开它的页面都要要花费6 秒:同时另外一个网站提供了相似的服务,但响应时间只需3 秒,那么你会如何选择呢?数据表 ...
- 用 NodeJS 实现 BigPipe
BigPipe 是 Facebook 开发的优化网页加载速度的技术.网上几乎没有用 node.js 实现的文章,实际上,不止于 node.js,BigPipe 用其他语言的实现在网上都很少见.以至于这 ...
- 网站前端优化技术 BigPipe分块处理技术
前端优化已经到极致了么?业务还在为看到不停的而揪心么?还在为2秒率不达标苦恼么? 好吧我知道答案,大家一如既往的烦恼中... 那么接下来我们看看,facebook,淘宝,人人网,一淘都是怎么做前端优化 ...
- BigPipe 大的页面分割成一个一个管道
bigpipe创新驱动力 node实现 具体实现 过去十年,现代web站点变得更加动态和内容化,交互性也逐步增强, 传统的页面处理的方式却没有保持一样的速度发展,越来越不能满足用户对极致性能的追求. ...
- 在PowerShell中使用curl(Invoke-WebRequest)
前言 习惯了windows的界面模式就很难转去命令行,甚至以命令行发家的git也涌现出各种界面tool.然而命令行真的会比界面快的多,如果你是一个码农. situation:接到需求分析bug,需要访 ...
- php中实现的一个curl批处理的实例
curl是利用URL语法在命令行方式下工作的开源文件传输工具 本文在php中实现了的一个curl批处理的实例. 代码如下: header("Content-Type:text/html;ch ...
- PHP curl 函数
转载http://sunking.sinaapp.com/archives/111 最近使用curl的时候,发现了一个比较好用的函数,当然是初级者适用的一个函数,就是curl_getinfo(), 在 ...
- curl -w,–write-out参数详解
顾名思义,write-out的作用就是输出点什么.curl的-w参数用于在一次完整且成功的操作后输出指定格式的内容到标准输出. 输出格式由普通字符串和任意数量的变量组成,输出变量需要按照%{varia ...
随机推荐
- convert 时间转换
格式:CONVERT(data_type,expression[,style])说明:此样式一般在时间类型(datetime,smalldatetime)与字符串类型(nchar,nvarchar,c ...
- 《Linux命令行与shell脚本编程大全》 第二十七章 学习笔记
第二十七章:shell脚本编程进阶 监测系统统计数据 系统快照报告 1.运行时间 uptime命令会提供以下基本信息: 当前时间 系统运行的天数,小时数,分钟数 当前登录到系统的用户数 1分钟,5分钟 ...
- [Angular 2] ng-class and Encapsulated Component Styles
import {Input, Component, View, NgClass} from "angular2/angular2"; @Component({ selector: ...
- OSChina 其中很重要的一类——RequestContext
RequestContext 这个类在 OSChina 中是很重要的一个类.该类由全局 Filter 进行初始化.并传递给包含 Action 和 页面中直接使用.使用时通过 RequestContex ...
- iOS iOS8新特性--UIPopoverPresentationController
1.回顾UIPopoverController的使用,下面这份代码只能在ipad下运行 // 初始化控制器,SecondViewController类继承自UIViewController Secon ...
- yii自动登陆的验证机制浅析
一直在使用yii进行开发, 也知道如何去使用, 也仅仅是知道怎么去用罢了, 终归是没研究过源码, 心里发虚, 今天遇到一个问题, 关于自动登陆的问题. 要求就是, 修改登陆保存session天数为自定 ...
- ACM vim配置
ACM现场赛时用的,比较简短,但是主要的功能都有了. 直接打开终端输入gedit ~/.vimrc 把下面的东西复制到里面就行了. filetype plugin indent on colo eve ...
- python-property、docstring--笔记
已经有人总结的非常详细,而且写得不错,就直接贴过来用了 property作为装饰器函数,负责把一个方法变成属性调用的 廖雪峰关于property的讲解 http://www.liaoxuefeng.c ...
- ASP.NET5 静态文件
静态文件,包括HTML文件,CSS文件,图像文件和JavaScript文件,它是一个应用里所包含的资源. 1. 提供静态文件 默认的,静态文件存储在你的webroot目录下面,webroot的路径定义 ...
- propertychange input change
IE678 支持propertychange事件,可以监听所有属性(包括自定义属性)的改变事件,包括手动修改输入框文本以及js修改输入框文本. propertychange事件有个特点就是不支持冒泡, ...