PHP如何读取JAVA用gzip压缩返回的字节流

来源:互联网 发布:oracle 删数据 sql 编辑:程序博客网 时间:2024/05/21 13:15

最近一个项目需要使用PHP调用JAVA提供的一个WEB接口,但是JAVA接口返回的内容是用gzip压缩的字节流。

这里只贴下基本的使用方法。

JAVA代码:

// 压缩  public static String compress(String str) throws IOException {    if (str == null || str.length() == 0) {      return str;    }    ByteArrayOutputStream out = new ByteArrayOutputStream();    GZIPOutputStream gzip = new GZIPOutputStream(out);    gzip.write(str.getBytes());    gzip.close();    return out.toString("ISO-8859-1");  }

PHP代码:

<?php  $content = file_get_contents('http://www.xxx.com/getCode/');  $content = mb_convert_encoding($content, 'ISO-8859-1','utf-8');  echo $content = gzdecode($content);?>

而gzdecode只适用于php>=5.4.0版本的,因此低版本就没法用了,不过可以用另一个:

<?php$content = file_get_contents('http://www.xxx.com/getCode/');$content = mb_convert_encoding($content, 'ISO-8859-1','utf-8');$content = substr($content, 10, -8);$content = gzinflate($content);?>







0 0