关于PHP 如何用 curl 读取 HTTP chunked 数据 对于 Web 服务器返回的 HTTP chunked 数据, 我们可能希望在每一个 chunk 返回时得到回调, 而不是所有的响应返回后再回调. 例如, 当服务器是 icomet 的时候. 在 PHP 中使用 curl 代码如下: \n"; } else { $out = "GET /?feed=rss2 HTTP/1.1\r\n"; $out .= "Host: blog.maxthon.cn\r\n"; $out .= "Connection: Close\r\n\r\n"; fwrite($fp, $out); while (!feof($fp)) { echo fgets($fp, 128); } fclose($fp); } ?> 返回http内容: Date: Mon, 29 Mar 2010 10:16:13 GMT Server: Apache/2.2.8 (Unix) mod_ssl/2.2.8 OpenSSL/0.9.8b PHP/5.2.6 X-Powered-By: PHP/5.2.6 X-Pingback: http://blog.maxthon.cn/xmlrpc.php Last-Modified: Wed, 03 Mar 2010 03:13:41 GMT ETag: "8f16b619f32188bde3bc008a60c2cc11" Keep-Alive: timeout=15, max=120 Connection: Keep-Alive Transfer-Encoding: chunked Content-Type: text/xml; charset=UTF-8 22de 2009年12月31日
1711

请注意上面那些标红的4个字符,它们每隔一段数据就会出现一次,但是用其他的方法如curl,file_get_contents等取回的数据则没有这些玩意。换成其他的网站来抓取,也只是少数的网站会出现这种情况,多方搜索无解后,我无意中看到了上面返回头中有这么一个声明:Transfer-Encoding: chunked,而常见的Content-lenght字段没有了。这个声明的大致的意思是传输编码为分段方式。 在Google上搜索该关键词,在维基百科上找到对这个声明的解释(由于没有中文版,我只能自己按照意思翻译): Chunked Transfer Encoding is a mechanism that allows HTTP messages to be split in several parts. This can be applied to both HTTP requests (from client to server) and HTTP responses (from server to client) 分块传输编码是一种机制,允许将HTTP消息分成几个部分传输。同时适用于HTTP请求(从客户端到服务器)和 HTTP响应(从服务器到客户端) For example, let us consider the way in which an HTTP server may transmit data to a client application (usually a web browser). Normally, data delivered in HTTP responses is sent in one piece, whose length is indicated by the Content-Length header field. The length of the data is important, because the client needs to know where the response ends and any following response starts. With chunked encoding, however, the data is broken up into a series of blocks of data and transmitted in one or more "chunks" so that a server may start sending data before it knows the final size of the content that it's sending. Often, the size of these blocks is the same, but this is not always the case. 例如,让我们考虑HTTP服务器可将数据传输到客户端应用程序(通常是一个网络浏览器)使用哪些方式。通常情况下,在HTTP响应数据是按照一整块发送给客户端的,数据的长度是由Content - Length头域表示。数据的长度很重要,因为客户需要知道在哪里响应结束和后面的响应何时启动。而使用Chunked编码方式,不管怎样,数据都会分割成一系列的数据块和一个或多个转发的“块”,因此服务器在知道内容的长度之前,就可以开始发送数据后。通常情况下,这些数据块的大小是一样的,但也并不是绝对的。 大概意思了解后,我们来看例子: Chunked编码使用若干个Chunk串连而成,由一个标明长度为0的chunk标示结束。每个Chunk分为头部和正文两部分,头部内容指定下一段正文的字符总数(十六进制的数字)和数量单位(一般不写),正文部分就是指定长度的实际内容,两部分之间用回车换行(CRLF)隔开。在最后一个长度为0的Chunk中的内容是称为footer的内容,是一些附加的Header信息(通常可以直接忽略)。具体的Chunk编码格式如下: 编过码的响应内容: HTTP/1.1 200 OK Content-Type: text/plain Transfer-Encoding: chunked 25 这是第一段数据 1A 然后这是第二段数据 0 解码的数据: 这是第一段内容,然后这是第二段数据 情况搞清楚了,那么我们怎么来解码这个编码后的数据呢? 在php官方手册fsockopen函数下面的评论中,已经有很多人提出了解决方法 方法1.