php,从fsockopen结果中,如何删除分块数据并获取数据

发布于 2024-10-16 16:20:23 字数 866 浏览 5 评论 0原文

响应的结果是这样的

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

森林迷了鹿 2024-10-23 16:20:23

数据以“块”的形式传输,即前面带有其长度的字节块。引用维基百科的 示例

HTTP/1.1 200 OK
Content-Type: text/plain
Transfer-Encoding: chunked

25
This is the data in the first chunk

1C
and this is the second one

3
con
8
sequence
0

这里唯一相关的标头是 Transfer-Encoding: chunked。您会看到第一行有一个十六进制数字。这告诉你在接下来的块中预计有多少字节 - 十六进制的 25 是十进制的 37,果然,37 个字符后有一个换行符,后面跟着另一个十六进制数字,其中包含后续块的字节数,依此类推,直到结束 - 最后一个块大小必须为 0,表示数据结束。 (块大小不是内容的一部分)。解码后的消息将是这样的:

This is the data in the first chunk
and this is the second one
consequence

编辑: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:

HTTP/1.1 200 OK
Content-Type: text/plain
Transfer-Encoding: chunked

25
This is the data in the first chunk

1C
and this is the second one

3
con
8
sequence
0

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:

This is the data in the first chunk
and this is the second one
consequence

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文