在javascript中压缩php和uncompress中的数据 [英] Compress data in php and uncompress in javascript

查看:128
本文介绍了在javascript中压缩php和uncompress中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Greerings all

Greerings all

有没有办法压缩从php(服务器)发送的数据,然后使用javascript(客户端)解压缩数据?

Is there a way to compress data sent from php (server) and then uncompress the data using javascript (client)?

感谢您

推荐答案

我必须同意@ Domenic的答案。 @ Nishchay Sharma is off off。

I have to agree with @Domenic's answer here. @Nishchay Sharma is way off.

我要添加的唯一的事情是,如果你想在每个脚本基础上这样做,而不是配置整个服务器压缩一切,通过使用PHP的gzencode()函数和头调用来完成你的目标是微不足道的:

The only thing I'll add is if you want to do this on a per-script basis rather than configuring your entire server to compress everything, it's trivial to accomplish your goal by using PHP's gzencode() function coupled with a header call:

http://www.php.net/manual/en/function.gzencode.php

例如,假设您通过对PHP页面的Ajax调用来检索大量数据。你可以配置PHP页面使用gzencode如下:

For instance, let's say you are retrieving a huge set of data via an Ajax call to a PHP page. You could configure the PHP page to use gzencode as follows:

<?php
$someBigString = gzencode('blahblah...blah');

header("Content-type: text/javascript");
header('Content-Encoding: gzip'); 

echo $someBigString;  
?>

(这过于简化了,当然,但我保持简单。)

(This is overly simplified, of course, but I'm keeping it simple.)

您的JS Ajax调用将拉下数据,查看gzip头,并自动解压缩。我个人使用这种技术用于Google地图的非常大的地理坐标数据集,在解压缩时可以有多个兆字节的大小。这不容易!

Your JS Ajax call will pull the data down, see the gzip header, and decompress it automagically. I personally use this technique for very large geo-coordinate data sets for Google Maps that can be many megabytes in size when uncompressed. It couldn't be easier!

这篇关于在javascript中压缩php和uncompress中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆