Google Closure Compiler with PHP

Today Google released their new Closure Compiler , you can use it to optimize and minify your javascripts.

 

Now i show you how to use the new Google Closure Compiler over the RESTful API with PHP5. First of all, you don’t need to install anything, we will connect the free API via cURL usually activated in PHP5.

The API (see reference ) resides under the following URL and requires four params:

http://closure-compiler.appspot.com/compile

compilation_level


is one of three options: WHITESPACE_ONLY, SIMPLE_OPTIMIZATIONS, ADVANCED_OPTIMIZATIONS,
i use simple optimizations, it don't need further config (like advanced) but is
better than whitespace only.

output_format
is "text" if you want compile a javascript

output_info
is "compiled_code" if you want compile a javascript

js_code
is your javascript source code, instead you can submit "code_url" param, an url
to a javascript-file

enough with theory, now the PHP code:



$script = file_get_contents('http://www.domain.com/scripts/script.js');
$ch = curl_init('http://closure-compiler.appspot.com/compile');

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'output_info=compiled_code&output_format=text&compilation_level=SIMPLE_OPTIMIZATIONS&js_code=' . urlencode($script));
$output = curl_exec($ch);
curl_close($ch);


i use it in a deployer script and replace the content of my script files with the compiled versions. you can tryout the compiler with a simple html-form

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值