PHP压缩字符串

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.

Instead of encoding it through json try simply converting that array to a string and sending it through POST

$encoded = base64_encode(serialize($arrayOfData));

Send that as a POST request to the server and decode it on the other side

$decoded = base64_decode(unserialize($receivedData));

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.

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.