Google Closure Compiler:编程访问问题

发布于 2024-10-11 10:15:10 字数 2836 浏览 5 评论 0 原文

我正在尝试访问 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 错误。

我缺少什么?

I'm attempting to access the Closure Compiler tool programmatically, but having issues both with PHP and JavaScript. Here is a quick and dirty PHP script I whipped up just to play around with the compilers' 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>";
}

The output I see is:

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.

I thought, maybe there's a problem with my cURL options. So I tried JavaScript (via a jQuery.post() call). I "jQuerify"d a random Firefox window and ran the following code in the Firebug console:

$.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);
  }
);

The "Net" panel shows a 403 error for this.

What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

錯遇了你 2024-10-18 10:15:10

根据 API 文档

The request must always have a Content-type header of application/x-www-form-urlencoded

在您的代码中没有看到

添加

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/x-www-form-urlencoded'
));

curl_exec() 之前

According to API docs

The request must always have a Content-type header of application/x-www-form-urlencoded

Didn't see that in your code

Add

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/x-www-form-urlencoded'
));

before curl_exec()

天暗了我发光 2024-10-18 10:15:10

由于同源策略,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..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文