程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> 關於PHP 如何用 curl 讀取 HTTP chunked 數據,curlchunked

關於PHP 如何用 curl 讀取 HTTP chunked 數據,curlchunked

編輯:關於PHP編程

關於PHP 如何用 curl 讀取 HTTP chunked 數據,curlchunked


對於 Web 服務器返回的 HTTP chunked 數據, 我們可能希望在每一個 chunk 返回時得到回調, 而不是所有的響應返回後再回調. 例如, 當服務器是 icomet 的時候.

在 PHP 中使用 curl 代碼如下:

<?php 
$url = "http://127.0.0.1:8100/stream";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'myfunc');
$result = curl_exec($ch);
curl_close($ch);
function myfunc($ch, $data){
$bytes = strlen($data);
// 處理 data
return $bytes;
}

但是, 這裡有一個問題. 對於一個 chunk, 回調函數可能會被調用多次, 每一次大概是 16k 的數據. 這顯然不是我們希望得到的. 因為 icomet 的一個 chunk 是以 "\n" 結尾, 所以回調函數可以做一下緩沖.

function myfunc($ch, $data){
$bytes = strlen($data);
static $buf = '';
$buf .= $data;
while(1){
$pos = strpos($buf, "\n");
if($pos === false){
break;
}
$data = substr($buf, 0, $pos+1);
$buf = substr($buf, $pos+1);
// 處理 data
}
}

下面給大家介紹下chunked php使用fsockopen讀取分段數據(transfer-encoding: chunked)

使用fsockopen讀取數據時遇到了一個神奇的問題,具體情況如下:

讀取地址: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><![CDATA[<p>2009年12月31日<br />
1711</p>

請注意上面那些標紅的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.

<?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原始數據(包括頭)

您可能感興趣的文章:

  • PHP的cURL庫功能簡介 抓取網頁、POST數據及其他
  • php中使用Curl、socket、file_get_contents三種方法POST提交數據
  • PHP下使用CURL方式POST數據至API接口的代碼
  • 解析PHP 使用curl提交json格式數據
  • php使用curl發送json格式數據實例
  • php curl模擬post提交數據示例
  • php使用curl和正則表達式抓取網頁數據示例
  • PHP函數分享之curl方式取得數據、模擬登陸、POST數據

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved