最新消息:

php让服务器不返回chunked

php admin 2866浏览 0评论

chunked是http头部一个很特殊的东西,现代浏览器都支持解析Transfer-Encoding:chunked,chunked能更快的让页面呈现出来,因为它能分段下载,浏览器分段解析,对于大页面,这显然提高了可用性,facebook利用这个特性实现了牛逼的big pipe,那实在是将chunked发挥到淋漓尽致的应用了。

nginx好像默认就支持chunked,我还没有找到选项关闭,现在我想要获取一个服务器上的数据,该服务器头部默认包含了chunked,这个特性导致用php处理起来显得有些不爽,而且即使他分段提供,我还是需要把它全部读完才开始处理,虽然php可以很方便的http_chunked_decode,但我这人骨子里就是有点喜欢反人类,让服务器不要返回chunked的数据不就可以了吗?后来。。。。。后来还真让我想到了,chunked是http 1.1才有的东西,所以如果告诉服务器,我只能解析1.0,应该就解决问题了,请看烂代码示例:

 

<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #000088;">$host</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'www.qq.com'</span><span style="color: #339933;">;</span>
<span style="color: #990000;">set_time_limit</span><span style="color: #009900;">(</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$st</span><span style="color: #339933;">=</span><span style="color: #990000;">array_sum</span><span style="color: #009900;">(</span><span style="color: #990000;">explode</span><span style="color: #009900;">(</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span><span style="color: #990000;">microtime</span><span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$ip</span><span style="color: #339933;">=</span><span style="color: #990000;">gethostbyname</span><span style="color: #009900;">(</span><span style="color: #000088;">$host</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$fp</span><span style="color: #339933;">=@</span><span style="color: #990000;">fsockopen</span><span style="color: #009900;">(</span><span style="color: #000088;">$ip</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">80</span><span style="color: #339933;">,</span><span style="color: #000088;">$errno</span><span style="color: #339933;">,</span><span style="color: #000088;">$errst</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">(</span> <span style="color: #339933;">!</span><span style="color: #000088;">$fp</span> <span style="color: #009900;">)</span><span style="color: #009900;">{</span>
	<span style="color: #990000;">die</span><span style="color: #009900;">(</span><span style="color: #0000ff;">'挂了.'</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #009900;">}</span>
<span style="color: #000088;">$header</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">"GET / HTTP/1.0<span style="color: #000099; font-weight: bold;">r</span><span style="color: #000099; font-weight: bold;">n</span>"</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$header</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">"Host: <span style="color: #006699; font-weight: bold;">{$host}</span><span style="color: #000099; font-weight: bold;">r</span><span style="color: #000099; font-weight: bold;">n</span>"</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$header</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">"User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0.1) Gecko/20100101 Firefox/7.0.1<span style="color: #000099; font-weight: bold;">r</span><span style="color: #000099; font-weight: bold;">n</span>"</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$header</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">"Accept: text/html<span style="color: #000099; font-weight: bold;">r</span><span style="color: #000099; font-weight: bold;">n</span>"</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$header</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">"Connection: keep-alive<span style="color: #000099; font-weight: bold;">r</span><span style="color: #000099; font-weight: bold;">n</span><span style="color: #000099; font-weight: bold;">r</span><span style="color: #000099; font-weight: bold;">n</span>"</span><span style="color: #339933;">;</span>
<span style="color: #990000;">fwrite</span><span style="color: #009900;">(</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #000088;">$header</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$c</span><span style="color: #339933;">=</span><span style="color: #990000;">fread</span><span style="color: #009900;">(</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span><span style="color: #cc66cc;">1024</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #990000;">var_dump</span><span style="color: #009900;">(</span><span style="color: #000088;">$c</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$et</span><span style="color: #339933;">=</span><span style="color: #990000;">array_sum</span><span style="color: #009900;">(</span><span style="color: #990000;">explode</span><span style="color: #009900;">(</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span><span style="color: #990000;">microtime</span><span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #990000;">fclose</span><span style="color: #009900;">(</span><span style="color: #000088;">$fp</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #990000;">var_dump</span><span style="color: #009900;">(</span><span style="color: #000088;">$et</span><span style="color: #339933;">-</span><span style="color: #000088;">$st</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span>

可以尝试把代码中的1.0改成1.1试试,body部分总是会多出那么几个数字,烦人。这里仅拿QQ做个示例,并非采集之用,给我留言乱猜的同学就此谢过了。

转载请注明:爱开源 » php让服务器不返回chunked

您必须 登录 才能发表评论!