Google Closure Compiler:编程访问问题
我正在尝试访问 Closure Compiler 工具 以编程方式,但 PHP 和 JavaScript 都存在问题。这是我编写的一个快速而肮脏的 PHP 脚本,只是为了使用编译器的 REST API:
<?php
if (!empty($_POST)) {
echo '<pre>';
print_r($_POST);
echo '</pre><br />';
foreach ($_POST as $k => &$v) $v = urlencode($v);
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST);
curl_setopt($ch, CURLOPT_URL, 'http://closure-compiler.appspot.com/compile');
echo curl_exec($ch);
} else {
echo "
<html>
<body>
<form action='' method='post'>
<p>Type JavaScript code to optimize here:</p>
<textarea name='js_code' cols='50' rows='5'>
function hello(name) {
// Greets the user
alert('Hello, ' + name);
}
hello('New user');
</textarea>
<input type='hidden' name='compilation_level' value='WHITESPACE_ONLY' />
<input type='hidden' name='output_format' value='json' />
<input type='hidden' name='output_info' value='compiled_code' />
<input type='hidden' name='warning_level' value='VERBOSE' />
<br /><br />
<input type='submit' value='Optimize' />
</form>
</body>
</html>";
}
我看到的输出是:
Array
(
[js_code] => function hello(name) {
// Greets the user
alert(\'Hello, \' + name);
}
hello(\'New user\');
[compilation_level] => WHITESPACE_ONLY
[output_format] => json
[output_info] => compiled_code
[warning_level] => VERBOSE
)
Error(13): No output information to produce, yet compilation was requested.
我想,也许我的 cURL 选项。所以我尝试了 JavaScript(通过 jQuery.post() 调用)。我“jQuerify”随机 Firefox 窗口并在 Firebug 控制台:
$.post('http://closure-compiler.appspot.com/compile',
{
'js_code': "function hello(name) {/*Greets the user*/alert('Hello, ' + name);}",
'compilation_level': 'SIMPLE_OPTIMIZATIONS',
'output_format': 'text',
'output_info': 'compiled_code'
},
function(response) {
alert(response);
}
);
“Net”面板显示 403 错误。
我缺少什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据 API 文档
在您的代码中没有看到
添加
在
curl_exec()
之前According to API docs
Didn't see that in your code
Add
before
curl_exec()
由于同源策略,Ajax(通过 jQuery 或其他)将无法工作。 (ajax 请求被限制在同一域中,除非预期 jsonp 作为结果)
只需使用您的示例来发布信息,它的工作原理如 http://www.jsfiddle.net/RySLr/
所以这一定是@German Rumm 提到的..
Ajax (via jQuery or otherwise) will not work because of same-origin policy. (ajax requests are restricted in same domain, unless jsonp is expected as result)
Simply using your example to post the info, it works as seen in http://www.jsfiddle.net/RySLr/
So it must be what @German Rumm mentions..