PHP 压缩字符串
我有一个数组转储,我想将其压缩为一个字符串,该字符串可以通过 url 发送到另一部分并进行解码。
目前我正在 gz 压缩它并对其进行 base64 编码。然而它似乎不起作用。当我在接收端使用它时,它返回 bool(false)
。
gzcompress(json_encode($images),9))
编码数据示例
string(214) "{"1":null,"2":null,"3":null,"4":"..\/uploads\/2462df38db374653720daa42b7aefec4\/cv4slm3fr8_c.png","5":"..\/uploads\/2462df38db374653720daa42b7aefec4\/vzh72hwqvk_c.png","6":null,"7":null,"8":null,"9":null,"10":null}"
null
值用类似数据填充。我需要通过 url 将此数据传递给另一个脚本。
注意:打开的网址位于模式框内,因此我无法发布到它。
I have an array dump that I want to compress into a string that can be sent over the url to another part and decoded.
Currently I am gzcompressing it and base64 encoding it. However it does not seem to be working. It is returning bool(false)
when I use it on the receiving end.
gzcompress(json_encode($images),9))
Example of encoded data
string(214) "{"1":null,"2":null,"3":null,"4":"..\/uploads\/2462df38db374653720daa42b7aefec4\/cv4slm3fr8_c.png","5":"..\/uploads\/2462df38db374653720daa42b7aefec4\/vzh72hwqvk_c.png","6":null,"7":null,"8":null,"9":null,"10":null}"
The null
values with be filled with similar data. And I need to pass this data via a url to another script.
Note: the url that gets opened is inside a modal box, hence why I cannot POST to it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要这样做。 URL 不适用于“大”字符串。每个浏览器的长度限制都不同。当浏览器/服务器默默地截断您的 URL 时,您可能会面临数据损坏的风险。 URL 也不适合任意二进制数据,这就是您最终得到的 GZ 压缩数据,您必须对它进行 url 编码,这几乎肯定会使字符串膨胀超过原来的值。
请改用 POST 请求,它有更大的限制。
Don't do this. URLs are not intended for 'large' strings. They have length limits that are different for every browser. You risk getting corrupted data when a browser/server silently truncates the URL on you. URLs are also not good for arbitrary binary data, which is what you'd end up with GZ-compressed data, you'd have to url-encode it, which would almost certainly balloon the string way past what it was originally.
Use a POST request instead, which has much larger limits.
不要通过 json 对其进行编码,而是尝试简单地将数组转换为字符串并通过 POST 发送它,
将其作为 POST 请求发送到服务器并在另一端对其进行解码
Instead of encoding it through json try simply converting that array to a string and sending it through POST
Send that as a POST request to the server and decode it on the other side
上面的答案涵盖了大部分内容。但是马克提到了很好的观点。
传递更大的字符串可能会给您带来问题。POST 是个好方法。
但有些情况下你不能使用post。例如,如果您想提供书签功能。
因此,要在字符串中传递此类数据,请使用 base64_encode(gzcompress(json_encode($arr)),9)。这肯定会减少查询字符串中传递的参数的大小。
希望这会有所帮助。
In above answers most of the things covered.But Marc have mentioned good point.
Passing bigger strings can cause you problems.POST is good way.
But there are some situations where you can't use post. e.g. if you want to provide the bookmark feature.
So to pass this kind of data in string use base64_encode(gzcompress(json_encode($arr)),9). That will definitely reduce the size of the parameters passed in query string.
Hope that will be helpful.