Created
July 6, 2018 08:50
-
-
Save tomjamescn/a2f129573615a429372b94e405996bb2 to your computer and use it in GitHub Desktop.
使用php处理http响应_兼容chunked和压缩的响应
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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