为了在 PHP 代码中解码/解压缩接收到的 HTTP POST 数据,请求来自 Java/Android 应用程序通过 HTTP POST GZIP/DEFLATE 压缩格式的数据
1) 从 Java Android 应用程序发送到 PHP 的数据使用 DeflaterOutputStream java 类,并在 PHP 中接收,如下所示
echo gzinflate( substr($HTTP_RAW_POST_DATA,2,-4) ) . PHP_EOL . PHP_EOL;
2) 从 Java Android 应用程序发送到 PHP 的数据使用 GZIPOutputStream java 类,并在 PHP 代码中接收,如下所示
echo gzinflate( substr($HTTP_RAW_POST_DATA,10,-8) ) . PHP_EOL . PHP_EOL;
从 Java Android 端 (API 级别 10+),数据以 DEFLATE 压缩格式发送
String body = "Lorem ipsum shizzle ma nizle";
URL url = new URL("http://www.url.com/postthisdata.php");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
conn.setRequestProperty("Content-encoding", "deflate");
conn.setRequestProperty("Content-type", "application/octet-stream");
DeflaterOutputStream dos = new DeflaterOutputStream(
conn.getOutputStream());
dos.write(body.getBytes());
dos.flush();
dos.close();
BufferedReader in = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String decodedString = "";
while ((decodedString = in.readLine()) != null) {
Log.e("dump",decodedString);
}
in.close();
在 PHP 端 (v 5.3.1),用于解压缩此 DEFLATE 数据的代码将是
echo substr($HTTP_RAW_POST_DATA,2,-4);
从 Java Android 端 (API 级别 10+),数据以 GZIP 压缩格式发送
String body1 = "Lorem ipsum shizzle ma nizle";
URL url1 = new URL("http://www.url.com/postthisdata.php");
URLConnection conn1 = url1.openConnection();
conn1.setDoOutput(true);
conn1.setRequestProperty("Content-encoding", "gzip");
conn1.setRequestProperty("Content-type", "application/octet-stream");
GZIPOutputStream dos1 = new GZIPOutputStream(conn1.getOutputStream());
dos1.write(body1.getBytes());
dos1.flush();
dos1.close();
BufferedReader in1 = new BufferedReader(new InputStreamReader(
conn1.getInputStream()));
String decodedString1 = "";
while ((decodedString1 = in1.readLine()) != null) {
Log.e("dump",decodedString1);
}
in1.close();
在 PHP 端 (v 5.3.1),用于解压缩此 GZIP 数据的代码将是
echo substr($HTTP_RAW_POST_DATA,10,-8);
有用的 PHP 代码,用于使用所有可用格式打印出压缩数据。
$data = "Lorem ipsum shizzle ma nizle";
echo "\n\n\n";
for($i=-1;$i<=9;$i++)
echo chunk_split(strtoupper(bin2hex(gzcompress($data,$i))),2," ") . PHP_EOL . PHP_EOL;
echo "\n\n\n";
for($i=-1;$i<=9;$i++)
echo chunk_split(strtoupper(bin2hex(gzdeflate($data,$i))),2," ") . PHP_EOL . PHP_EOL;
echo "\n\n\n";
for($i=-1;$i<=9;$i++)
echo chunk_split(strtoupper(bin2hex(gzencode($data,$i,FORCE_GZIP))),2," ") . PHP_EOL . PHP_EOL;
echo "\n\n\n";
for($i=-1;$i<=9;$i++)
echo chunk_split(strtoupper(bin2hex(gzencode($data,$i,FORCE_DEFLATE))),2," ") . PHP_EOL . PHP_EOL;
echo "\n\n\n";
希望这有帮助。如果您发现这节省了您大量的时间和精力,请点赞。