关于PHP 如何用 curl 读取 HTTP chunked 数据(2)

<?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原始数据(包括头)

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/c6d8ddd01cb7f1fa196f5ecd60256948.html