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"]."&nbsp;&nbsp;能正常打开";
echo "<script>update('$content_id', '$content')</script>";
} else {
$content = $url_info["host"]."&nbsp;&nbsp;访问失败。具体错误原因:".$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}&nbsp;&nbsp;检测中</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}秒&nbsp;&nbsp;<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等压缩。

转载于:https://www.cnblogs.com/guoyongrong/p/3338197.html

php通过curl实现bigpipe相关推荐

  1. BigPipe学习研究

    2019独角兽企业重金招聘Python工程师标准>>> 1. 技术背景 FaceBook页面加载技术 试想这样一个场景,一个经常访问的网站,每次打开它的页面都要要花费6 秒:同时另外 ...

  2. BigPipe 大的页面分割成一个一个管道

    bigpipe创新驱动力  node实现 具体实现 过去十年,现代web站点变得更加动态和内容化,交互性也逐步增强, 传统的页面处理的方式却没有保持一样的速度发展,越来越不能满足用户对极致性能的追求. ...

  3. php实现bigpipe

    BigPipe是facebook发明的一种页面加载技术.其实也不是什么新技术了,也有很多文章进行说明.但是在网上,具体讲如何使用php实现bigpipe的文章很少,并且有些文章还不很准确.bigpip ...

  4. 【转】BigPipe学习研究

    1. 技术背景 FaceBook页面加载技术 试想这样一个场景,一个经常访问的网站,每次打开它的页面都要要花费6 秒:同时另外一个网站提供了相似的服务,但响应时间只需3 秒,那么你会如何选择呢?数据表 ...

  5. BigPipe facebook

    1. 技术背景 FaceBook页面加载技术 试想这样一个场景,一个经常访问的网站,每次打开它的页面都要要花费6 秒:同时另外一个网站提供了相似的服务,但响应时间只需3 秒,那么你会如何选择呢?数据表 ...

  6. Bigpipe学习【转】

    转自:http://www.searchtb.com/2011/04/an-introduction-to-bigpipe.html 1. 技术背景 FaceBook页面加载技术 试想这样一个场景,一 ...

  7. curl模拟post请求

    另外可尝试 postman工具 或者用request 直接请求 CURL 发送POST请求curl -header "Content-Type: application/json" ...

  8. 在CentOS 6.8 x86_64上安装nghttp2最新版及让curl具有http2特性

    nghttp2是什么? nghttp2是一个实现IETF官方HTTP/2和HPACK头压缩算法的C库,但不限如此,在C库基础上,还实现了http2的客户端,服务器,代理服务器,以及压测工具.参见官网链 ...

  9. curl: (3) [globbing] error: bad range specification after pos 150的解决方法

    在线上服务器上执行下面的命令 curl -vo /dev/null 'http://120.52.72.46:80/fileshare3010.dfiles.eu/c3pr90ntcsf0/auth- ...

最新文章

  1. incident用法_“incident、accident、event”,都是“事件”,怎么区分?
  2. Linux0.11小结
  3. java二级考试简单应用题,计算机二级考试Java语言模拟考试(2)
  4. python流程图基本元素-2-2:python之控制结构
  5. 第一章--第一节:环境搭建
  6. 2020-11-23(dll注入方法)
  7. linux系统如何安装vesta,[linux服务器]安装Vesta Control Panel
  8. relative和absolute使用
  9. Struts2、Spring3、MyBatis3整合ExtJS,完成CheckNodeColumnTree
  10. 转 在SQL Server中创建用户角色及授权(使用SQL语句)
  11. visionpro加载toolblock 和保存toolblock
  12. 善用工具和网上资源-决定学习的效率
  13. 在地图上点击一下,在图层上画一个点,并显示相关的信息
  14. 单片机C语言控制16*16LED显示屏,基于单片机的pwm控制16*16led点阵亮度调节怎么做啊,...
  15. Emacs-224-彩虹猫的实现
  16. AUTOSAR E2E SecOC Comparison
  17. shell中source、sh、bash、./的区别
  18. 计算机一级考试试题分值怎么样的,计算机一级考试的试题分值怎么样的?
  19. ios调用微信支付提示当前页面的URL未注册
  20. speedoffice(PPT)插入的图片如何裁剪呢?

热门文章

  1. TransactionSynchronizationManager用法和含义
  2. 【网络编程】Linux tcpdump命令详解---编辑中
  3. 输入五个城市从小到大排序-c语言
  4. ISTQB-TM-大纲
  5. Leetcode 977.有序数组的平方
  6. 淘宝天猫运营,淘宝客推广,店铺流量提升
  7. 对文件进行的操作命令
  8. 机器指令和微指令的关系
  9. multimap 的使用例子
  10. ubuntu修改用户名和home对应的目录名