设为首页 收藏本站
查看: 2405|回复: 0

[经验分享] PHP解码chunked编码的数据

[复制链接]

尚未签到

发表于 2017-4-4 10:52:33 | 显示全部楼层 |阅读模式
  在使用fsockopen获取网页内容时,服务端有时会返回chunked编码(头信息为Transfer-Encoding: chunked)的数据,新版的PHP中并不包含http_chunked_decode函数,根据HTTP协议chunked编码的规则,不难自己实现http_chunked_decode函数的功能。
  <?php
/**
* 解码chunked数据
* @param string $data
* @return string
*/
function http_chunked_decode($data)
{
$pos=0;
$temp='';
while($pos<strlen($data))
{
// chunk部分(不包含CRLF)的长度,即"chunk-size [ chunk-extension ]"
$len=strpos($data,"/r/n",$pos)-$pos;
// 截取"chunk-size [ chunk-extension ]"
$str=substr($data,$pos,$len);
// 移动游标
$pos+=$len+2;
// 按;分割,得到的数组中的第一个元素为chunk-size的十六进制字符串
$arr=explode(';',$str,2);
// 将十六进制字符串转换为十进制数值
$len=hexdec($arr[0]);
// 截取chunk-data
$temp.=substr($data,$pos,$len);
// 移动游标
$pos+=$len+2;
}
return $temp;
}
// example
$string = "".
"05/r/n".
"This /r/n".
"07/r/n".
"string /r/n".
"12/r/n".
"is chunked encoded/r/n".
"01/r/n".
"!/r/n".
"00/r/n";
echo '<pre>';
echo $string;
echo http_chunked_decode($string);
?>
  附W3C官网chunked编码规则:


3.6.1 Chunked Transfer Coding

  The chunked encoding modifies the body of a message in order to transfer it as a series of chunks, each with its own size indicator, followed by an OPTIONAL trailer containing entity-header fields. This allows dynamically produced content to be transferred along with the information necessary for the recipient to verify that it has received the full message.

       Chunked-Body   = *chunk
last-chunk
trailer
CRLF

       chunk          = chunk-size [ chunk-extension ] CRLF
chunk-data CRLF
chunk-size     = 1*HEX
last-chunk     = 1*("0") [ chunk-extension ] CRLF

       chunk-extension= *( ";" chunk-ext-name [ "=" chunk-ext-val ] )
chunk-ext-name = token
chunk-ext-val  = token | quoted-string
chunk-data     = chunk-size(OCTET)
trailer        = *(entity-header CRLF)

  The chunk-size field is a string of hex digits indicating the size of the chunk. The chunked encoding is ended by any chunk whose size is zero, followed by the trailer, which is terminated by an empty line.
  The trailer allows the sender to include additional HTTP header fields at the end of the message. The Trailer header field can be used to indicate which header fields are included in a trailer (see section 14.40).
  A server using chunked transfer-coding in a response MUST NOT use the trailer for any header fields unless at least one of the following is true:
  a)the request included a TE header field that indicates "trailers" is acceptable in the transfer-coding of the response, as described in section 14.39; or,
  b)the server is the origin server for the response, the trailer fields consist entirely of optional metadata, and the recipient could use the message (in a manner acceptable to the origin server) without receiving this metadata. In other words, the origin server is willing to accept the possibility that the trailer fields might be silently discarded along the path to the client.
  This requirement prevents an interoperability failure when the message is being received by an HTTP/1.1 (or later) proxy and forwarded to an HTTP/1.0 recipient. It avoids a situation where compliance with the protocol would have necessitated a possibly infinite buffer on the proxy.
  An example process for decoding a Chunked-Body is presented in appendix 19.4.6.
  All HTTP/1.1 applications MUST be able to receive and decode the "chunked" transfer-coding, and MUST ignore chunk-extension extensions they do not understand.
  附W3C官网chunked解码伪代码:


19.4.6 Introduction of Transfer-Encoding

  HTTP/1.1 introduces the Transfer-Encoding header field (section 14.41). Proxies/gateways MUST remove any transfer-coding prior to forwarding a message via a MIME-compliant protocol.
  A process for decoding the "chunked" transfer-coding (section 3.6) can be represented in pseudo-code as:

       length := 0
read chunk-size, chunk-extension (if any) and CRLF
while (chunk-size > 0) {
read chunk-data and CRLF
append chunk-data to entity-body
length := length + chunk-size
read chunk-size and CRLF
}
read entity-header
while (entity-header not empty) {
append entity-header to existing header fields
read entity-header
}
Content-Length := length
Remove "chunked" from Transfer-Encoding

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-359981-1-1.html 上篇帖子: 利用PHP+JavaScript打造AJAX搜索 下篇帖子: php中使用Content-type类别列表
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表