Solved

How to gzip JSON from PHP using AJAX?

Posted on 2013-12-09
10
2,283 Views
Last Modified: 2013-12-11
Hi,

I'm wondering how I can gzip json data from PHP with an AJAX call.

I was reading here that in PHP 5.4 you can use:

json_encode($data,JSON_UNESCAPED_UNICODE);

Unfortunately, I don't have PHP 5.4 installed right now.

So the other user suggested:

ob_start('ob_gzhandler');

But that just doesn't work when I place that code at the top of my PHP file. Server returns error.

I'm looking for an alternative to:
json_encode($data,JSON_UNESCAPED_UNICODE);

and also how would I unzip the json data in the $.ajax call:

$.ajax({
    type : 'post',
    url: './model/06_modCheckPrimaryBeneficiaries' + sAppendDateTime + '.php',
    cache: false,
    dataType: 'json',
    success: function(retData, textStatus, jqXHR) {
        var test = retData;
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
        console.log('textStatus: '+textStatus);
    });

Open in new window

0
Comment
Question by:Victor Kimura
  • 4
  • 3
  • 3
10 Comments
 
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 250 total points
ID: 39706636
If you are talking about delivering your JSON in 'gzip' format to the browser, that is not something that you choose or do.  That is a negotiation between the browser and the web server.

In the HTTP request from the browser, the browser tells the server what formats it can accept in the response.  In the HTTP response (when 'gzip' is acceptable), the server checks the file size to determine whether it would be quicker to just send a small file or gzip and send a larger file and then does what seems to be the best choice.
0
 

Author Comment

by:Victor Kimura
ID: 39706662
Oh, okay.

So, this code:
json_encode($data,JSON_UNESCAPED_UNICODE);

isn't really gzipping it then?

Is that true of some .js files too? I have another post here that you may be able to answer too:
http://www.experts-exchange.com/Software/Server_Software/Web_Servers/Apache/Q_28313939.html
0
 
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 250 total points
ID: 39706688
The 'gzip' method of sending data from the server to the browser is the same for all file types.  You can 'gzip' a file for storage on your server but that is a different thing.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:Victor Kimura
ID: 39706746
Ok, thanks.

just wondering if you know if this code:
json_encode($data,JSON_UNESCAPED_UNICODE);

will make the json data smaller or more efficient then just regular encoding.
0
 
LVL 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 39708321
If the JSON is so large that making it smaller is a worthwhile objective, the application is misdesigned.  JSON_UNESCAPED_UNICODE is not related to GZIP.  PHP says: "Encode multibyte Unicode characters literally (default is to escape as \uXXXX). Available since PHP 5.4.0."

The JSON standard requires UTF-8.  PHP has a long history of thinking that a byte == a character, and this is not true in UTF-8, where characters can be made up from multiple bytes.  So my guess is that PHP was encoding the Unicode characters to try to play nice with legacy applications and avoid breaking the JSON standard.

See these links for more info on Unicode:
http://php.net/manual/en/json.constants.php
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11880-Unicode-PHP-and-Character-Collisions.html
0
 

Author Comment

by:Victor Kimura
ID: 39709351
Ok, I see. How big is too big? I have a some form fields that return in JSON format. The user can add several beneficiaries (it's for estate planning) to this particular page and each beneficiary the user adds increases the JSON by about 24 KB. So, if the user adds in 3 beneficiaries, then that JSON return is about 72 KB. Is that too large? I just want to see how I can optimize the JSON and what's considered too large. I'm trying to see how I can make this more efficient.

Thank you for your input.
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 250 total points
ID: 39709399
72KB is not too large, nor is ten times that.  I was thinking more like the size of the Amazon.com catalog as being too large.

If you're finding timeouts when you submit the form, you might consider a design that used AJAX to submit the form in parts.
0
 

Author Closing Comment

by:Victor Kimura
ID: 39711899
Thanks so much for your valuable input. Both of you! =)
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 39711959
Glad we could help.  Thanks for the points and thanks for using EE, ~Ray
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39712076
You're welcome, glad to help.
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
comma true 6 33
writing a post to a file that is on same folder and same webserver 5 31
DataTables + iCheck + pagination Issue 2 23
Javascript Form Change 5 23
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question