在 PHP 中壓縮 JSON 字符串並在 Javascript 中解壓縮以進行 Google API 的數據庫查詢 (Compress JSON string in PHP and decompress in Javascript for Database query for Google API)


問題描述

在 PHP 中壓縮 JSON 字符串並在 Javascript 中解壓縮以進行 Google API 的數據庫查詢 (Compress JSON string in PHP and decompress in Javascript for Database query for Google API)

I generate a JSON files in my PHP code (by running a database query). This JSON file is downloaded on the client side in my Javascript using the Data Queries example of the Google Chart Tools:

function initialize() {
  // Replace the data source URL on next line with your data source URL.
  // Specify that we want to use the XmlHttpRequest object to make the query.
  var opts = {sendMethod: 'xhr'};
  var query = new google.visualization.Query('http://spreadsheets.google.com?key=123AB&...', opts);

  // Optional request to return only column C and the sum of column B, grouped by C members.
  query.setQuery('select C, sum(B) group by C');

  // Send the query with a callback function.
  query.send(handleQueryResponse);
}

function handleQueryResponse(response) {

  if (response.isError()) {
    alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
    return;
  }

  var data = response.getDataTable();
  var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
  chart.draw(data, {width: 400, height: 240, is3D: true});
}

The JSON files are rather large. What is the best way to compress the JSONstring in PHP and decompress it in Javascript? 


參考解法

方法 1:

<p>@Gavin gave the right answer in a comment above:

His answer was: Look into GZIP'ing your content. If your server is setup correctly, it can compress any application/json content and then your browser "should" automatically decompress it. a&nbsp;href="http://bearpanther.com/2012/04/11/gzip-json-generated-on-the-fly"&nbsp;rel="nofollow"http://bearpanther.com/2012/04/11/gzip-json-generated-on-the-fly</a></p>

方法 2:

The question is "How to compress JSON with PHP". The answer is use ob_gzhandler. Use this approach only, if you do not have access to the server configuration.

I suggest the following solution, where you don't have to write any PHP code and get gzip on all your json resources:

change your server configuration to allow automatical gzip'ing of negotiated content

Apache .htaccess

`AddOutputFilterByType DEFLATE text/html ... application/json`

Nginx server.conf

gzip             on; // if not already set
gzip_comp_level  9;
gzip_types       application/json;

Example with more gzip_types:

gzip_types text/text text/html text/plain text/xml 
           text/css application/x-javascript application/javascript 
           application/json;

(by RuutRuutJens A. Koch)

參考文件

  1. Compress JSON string in PHP and decompress in Javascript for Database query for Google API (CC BY-SA 3.0/4.0)

#google-api #javascript #google-visualization #JSON #PHP






相關問題

在 PHP 中壓縮 JSON 字符串並在 Javascript 中解壓縮以進行 Google API 的數據庫查詢 (Compress JSON string in PHP and decompress in Javascript for Database query for Google API)

將 Google Places API 與 MonoTouch 一起使用? (Using Google Places API with MonoTouch?)

如何獲取我的 Gmail 帳戶的個人資料圖片? (How to get the profile picture of my Gmail account?)

google.elements.newsShow 顯示時間不起作用 (google.elements.newsShow display Time not working)

gapi.client.load 未調用回調:console.log 中指定了無效或非法字符串錯誤 (gapi.client.load not calling callback: An invalid or illegal string was specified error in console.log)

Google Developer Console 和已安裝的應用 (Google Developer Console and Installed App)

如何從 Google 自定義搜索 API 獲得 100 多個結果 (How to get more than 100 results from Google Custom Search API)

Pandas / Google Analytics API 身份驗證嘗試給我帶來了一個奇怪的 python 錯誤 (Pandas / Google Analytics API authentication attempt throws me a weird python error)

在 alpha 階段使用谷歌云功能進行生產 (Using a google cloud feature in alpha stage for production)

流量中的 Google Maps Distance Matrix API 持續時間添加返回錯誤結果的所有段 (Google Maps Distance Matrix API duration in traffic adding all segments returning wrong result)

Python 事件集成中的 QML Calendar 和 Google Calendar API (QML Calendar and Google Calendar API in Python events integration)

獲取錯誤 {“error”:“invalid_grant”,“error_description”:“令牌已過期或撤銷。” 來自谷歌 oauth2 API (Getting error { "error" : "invalid_grant", "error_description" : "Token has been expired or revoked." } from Google oauth2 API)







留言討論