在使用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编码规则:
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解码伪代码:
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