php,从fsockopen结果中,如何删除分块数据并获取数据
响应的结果是这样的
HTTP/1.1 200 OK
Date: Fri, 11 Feb 2011 06:59:47 GMT
Server: Apache
Set-Cookie: id%22%3Bs%3A32%3A%223c38c56b3def2530014336c922ee0bfc%22%3Bs%3A10%3A%22ip_address%22%3Bs%3A14%3A%2269.162.119.226%22%3Bs%3A10%3A%22user_agent%22%3Bs%3A0%3A%22%22%3Bs%3A13%3A%22last_activity%22%3Bs%3A10%3A%221297407587%22%3B%7Dcd356ca12b8d395b49603cb3eb34f786; expires=Fri, 11-Feb-2011 08:59:47 GMT; path=/ Set-Cookie: vaave_session=a%3A3Bs%3A32%3A%223c38c56b3def2530014336c922ee0bfc%22%3Bs%3A10%3A%22ip_address%22%3Bs%3A14%3A%2269.162.119.226%22%3Bs%3A10%3A%22user_agent%22%3Bs%3A0%3A%22%22%3Bs%3A13%3A%22last_activity%22%3Bs%3A10%3A%221297407587%22%3Bs%3A2%3A%22tz%22%3Bs%3A13%3A%22Asia%2FCalcutta%22%3B%7Daadf6cb5ad21eae3c04e24cf00b3ea16; expires=Fri, 11-Feb-2011 08:59:47 GMT; path=/
Connection: close
Transfer-Encoding: chunked
"data here"
the result of reponse is like this
HTTP/1.1 200 OK
Date: Fri, 11 Feb 2011 06:59:47 GMT
Server: Apache
Set-Cookie: id%22%3Bs%3A32%3A%223c38c56b3def2530014336c922ee0bfc%22%3Bs%3A10%3A%22ip_address%22%3Bs%3A14%3A%2269.162.119.226%22%3Bs%3A10%3A%22user_agent%22%3Bs%3A0%3A%22%22%3Bs%3A13%3A%22last_activity%22%3Bs%3A10%3A%221297407587%22%3B%7Dcd356ca12b8d395b49603cb3eb34f786; expires=Fri, 11-Feb-2011 08:59:47 GMT; path=/ Set-Cookie: vaave_session=a%3A3Bs%3A32%3A%223c38c56b3def2530014336c922ee0bfc%22%3Bs%3A10%3A%22ip_address%22%3Bs%3A14%3A%2269.162.119.226%22%3Bs%3A10%3A%22user_agent%22%3Bs%3A0%3A%22%22%3Bs%3A13%3A%22last_activity%22%3Bs%3A10%3A%221297407587%22%3Bs%3A2%3A%22tz%22%3Bs%3A13%3A%22Asia%2FCalcutta%22%3B%7Daadf6cb5ad21eae3c04e24cf00b3ea16; expires=Fri, 11-Feb-2011 08:59:47 GMT; path=/
Connection: close
Transfer-Encoding: chunked
"data here"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
数据以“块”的形式传输,即前面带有其长度的字节块。引用维基百科的 示例:
这里唯一相关的标头是
Transfer-Encoding: chunked
。您会看到第一行有一个十六进制数字。这告诉你在接下来的块中预计有多少字节 - 十六进制的 25 是十进制的 37,果然,37 个字符后有一个换行符,后面跟着另一个十六进制数字,其中包含后续块的字节数,依此类推,直到结束 - 最后一个块大小必须为 0,表示数据结束。 (块大小不是内容的一部分)。解码后的消息将是这样的:编辑:PECL 中似乎有一个现有的函数,http_chunked_decode() - 该页面上也有一个纯 PHP 实现 - 只需将分块数据传递给它,它就会尝试“解块”它。
The data is transmitted in "chunks", i.e. a block of bytes that's preceded by its length. To quote Wikipedia's example:
The only relevant header here is
Transfer-Encoding: chunked
. You'll see that there is a hex number on the first line. That tells you how many bytes to expect in the following chunk - 25 in hex is 37 in decimal, and sure enough, there's a linebreak after 37 chars, followed by another hex number with the byte count of the following chunk, and so on until the end - the last chunk size MUST be 0, signifying end of data. (The chunk sizes are not part of the content). The decoded message would be this:Edit: It seems that there is an existing function in PECL for this, http_chunked_decode() - and there's a pure PHP implementation on that page, too - just pass it the chunked data, and it will try to "unchunk" it.