使用fsockopen讀取數據時(shí)遇到了一個(gè)神奇的問(wèn)題,具體情況如下:
讀取地址:http://blog.maxthon.cn/?feed=rss2
讀取代碼:
<?php
$fp = fsockopen("blog.maxthon.cn", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\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
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
<description><![CDATA[2009年12月31日
1711
.......
1fe8
]]></description>
<content:encoded><有中文版,我只能自己按照意思翻譯):
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消息分成幾個(gè)部分傳輸。同時(shí)適用于HTTP請求(從客戶(hù)端到服務(wù)器)和 HTTP響應(從服務(wù)器到客戶(hù)端)
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服務(wù)器可將數據傳輸到客戶(hù)端應用程序(通常是一個(gè)網(wǎng)絡(luò )瀏覽器)使用哪些方式。通常情況下,在HTTP響應數據是按照一整塊發(fā)送給客戶(hù)端的,數據的長(cháng)度是由Content - Length頭域表示。數據的長(cháng)度很重要,因為客戶(hù)需要知道在哪里響應結束和后面的響應何時(shí)啟動(dòng)。而使用Chunked編碼方式,不管怎樣,數據都會(huì )分割成一系列的數據塊和一個(gè)或多個(gè)轉發(fā)的“塊”,因此服務(wù)器在知道內容的長(cháng)度之前,就可以開(kāi)始發(fā)送數據后。通常情況下,這些數據塊的大小是一樣的,但也并不是絕對的。
大概意思了解后,我們來(lái)看例子:
Chunked編碼使用若干個(gè)Chunk串連而成,由一個(gè)標明長(cháng)度為0的chunk標示結束。每個(gè)Chunk分為頭部和正文兩部分,頭部?jì)热葜付ㄏ乱欢握牡淖址倲担ㄊM(jìn)制的數字)和數量單位(一般不寫(xiě)),正文部分就是指定長(cháng)度的實(shí)際內容,兩部分之間用回車(chē)換行(CRLF)隔開(kāi)。在最后一個(gè)長(cháng)度為0的Chunk中的內容是稱(chēng)為footer的內容,是一些附加的Header信息(通??梢灾苯雍雎裕?。具體的Chunk編碼格式如下:
編過(guò)碼的響應內容:
HTTP/1.1 200 OK
Content-Type: text/plain
Transfer-Encoding: chunked
25
這是第一段數據
1A
然后這是第二段數據
0
解碼的數據:
這是第一段內容,然后這是第二段數據
情況搞清楚了,那么我們怎么來(lái)解碼這個(gè)編碼后的數據呢?
在php官方手冊fsockopen函數下面的評論中,已經(jīng)有很多人提出了解決方法
方法1.
<?php
function unchunk($result) {
return preg_replace_callback(
'/(?:(?:\r\n|\n)|^)([0-9A-F]+)(?:\r\n|\n){1,2}(.*?)'.
'((?:\r\n|\n)(?:[0-9A-F]+(?:\r\n|\n))|$)/si',
create_function(
'$matches',
'return hexdec($matches[1]) == strlen($matches[2]) ? $matches[2] : $matches[0];'
),
$result
);
}
方法二.
function unchunkHttp11($data) {
$fp = 0;
$outData = "";
while ($fp < strlen($data)) {
$rawnum = substr($data, $fp, strpos(substr($data, $fp), "\r\n") + 2);
$num = hexdec(trim($rawnum));
$fp += strlen($rawnum);
$chunk = substr($data, $fp, $num);
$outData .= $chunk;
$fp += strlen($chunk);
}
return $outData;
}
方法三.
HTTP/1.1 換成 HTTP/1.0
注意:這兩個(gè)函數的參數都是返回的http原始數據(包括頭)
聯(lián)系客服