Skip to content

Instantly share code, notes, and snippets.

@tomjamescn
Created July 6, 2018 08:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomjamescn/a2f129573615a429372b94e405996bb2 to your computer and use it in GitHub Desktop.
Save tomjamescn/a2f129573615a429372b94e405996bb2 to your computer and use it in GitHub Desktop.
使用php处理http响应_兼容chunked和压缩的响应
<?php
function http_unchunk($data) {
$res=[];
$p=0; $n=strlen($data);
while($p<$n) {
if (preg_match("/^([0-9A-Fa-f]+)\r\n/",substr($data,$p,18),$m)) {
$sz=hexdec($m[1]); $p+=strlen($m[0]);
$res[]=substr($data,$p,$sz); $p+=$sz+2;
} else {
break;
}
}
return implode('',$res);
}
//if Content-Encoding is gzip or x-gzip or x-compress use gzdecode
//if Content-Encoding is deflate use gzdeflate
if ($chunked) $body=http_unchunk($body);
if ($gzip) $body=gzdecode($body);
if ($deflate) $body=gzdeflate($body);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment