Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to gzip JSON from PHP using AJAX?

Posted on 2013-12-09
10
Medium Priority
?
2,576 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
10 Comments
 
LVL 84

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 1000 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 84

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 1000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1000 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 111

Accepted Solution

by:
Ray Paseur earned 1000 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 111

Expert Comment

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

Expert Comment

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

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

719 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