Home > Backend Development > PHP Tutorial > PHP中GZIP解压 自定义gzdecode函数的使用

PHP中GZIP解压 自定义gzdecode函数的使用

WBOY
Release: 2016-06-20 13:01:18
Original
3993 people have browsed it

在做一个微博项目中,发布视频需要解析外链获取视频地址信息,

用的是PHP的curl 获取内容,正则解析。

 后来测试发现土豆的视频无法解析,其他的都好。

经过反复测试,发现是土豆用了GZIP压缩,于是用gzdecode反解压。

发现本地的wamp 2.1 zlib 库, 网上cn.php.net 搜了解决办法,自定义个函数用gzinflate实现。

下面是代码

if (!function_exists('gzdecode')) {
    function gzdecode ($data) {
        $flags = ord(substr($data, 3, 1));
        $headerlen = 10;
        $extralen = 0;
        $filenamelen = 0;
        if ($flags & 4) {
            $extralen = unpack('v' ,substr($data, 10, 2));
            $extralen = $extralen[1];
            $headerlen += 2 + $extralen;
        }
        if ($flags & 8) // Filename
            $headerlen = strpos($data, chr(0), $headerlen) + 1;
        if ($flags & 16) // Comment
            $headerlen = strpos($data, chr(0), $headerlen) + 1;
        if ($flags & 2) // CRC at end of file
            $headerlen += 2;
        $unpacked = @gzinflate(substr($data, $headerlen));
        if ($unpacked === FALSE)
            $unpacked = $data;
        return $unpacked;
    }
}
Copy after login

希望对大家有用!


Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template