Google Closure 编译器和 multipart/form-data 不起作用

发布于 2024-12-29 00:57:15 字数 1265 浏览 3 评论 0 原文

我正在向 google 闭包编译器 API 服务发出请求:

   $content = file_get_contents('file.js');

   $url = 'http://closure-compiler.appspot.com/compile';
   $post = true;
   $postData = array('output_info' => 'compiled_code', 'output_format' => 'text', 'compilation_level' => 'SIMPLE_OPTIMIZATIONS', 'js_code' => urlencode($content)));

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);

    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    if ($post) {
        curl_setopt($ch, CURLOPT_POST, $post);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
    }

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

但是请求失败,我从 google 收到此错误消息:

   Error(18): Unknown parameter in Http request: '------------------------------0f1f2f05fb97
   Content-Disposition: form-data; name'.
   Error(13): No output information to produce, yet compilation was requested.

我查看了标头,并且正在发送此 Content-Type 标头:

  application/x-www-form-urlencoded; charset=UTF-8; boundary=----------------------------0f1f2f05fb97

不确定添加的边界是否是普通的?由于谷歌似乎不喜欢它,我该如何防止这种情况?

谢谢你, 韦斯利

I'm making a request to the google closure compiler API service:

   $content = file_get_contents('file.js');

   $url = 'http://closure-compiler.appspot.com/compile';
   $post = true;
   $postData = array('output_info' => 'compiled_code', 'output_format' => 'text', 'compilation_level' => 'SIMPLE_OPTIMIZATIONS', 'js_code' => urlencode($content)));

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);

    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    if ($post) {
        curl_setopt($ch, CURLOPT_POST, $post);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
    }

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

But the request is failing and I get this error message from google:

   Error(18): Unknown parameter in Http request: '------------------------------0f1f2f05fb97
   Content-Disposition: form-data; name'.
   Error(13): No output information to produce, yet compilation was requested.

I looked at the headers and this Content-Type header is being sent:

  application/x-www-form-urlencoded; charset=UTF-8; boundary=----------------------------0f1f2f05fb97

Not sure if that added boundary is normal? And how do I prevent this as google doesn't seem to like it?

Thank you,
Wesley

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

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

发布评论

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

评论(3

诗酒趁年少 2025-01-05 00:57:15

您必须使用 http_build_query()< /a> 在将 POST 数据(数组)发送到 cURL 之前。

string http_build_query ( mixed $query_data [, string $numeric_prefix [, string $arg_separator [, int $enc_type = PHP_QUERY_RFC1738 ]]] )

所以你的 $postData 应该是这样的:

$postData = http_build_query(
                                array(
                                'output_info' => 'compiled_code', 
                                'output_format' => 'text', 
                                'compilation_level' => 'SIMPLE_OPTIMIZATIONS', 
                                'js_code' => urlencode($content)
                                )
                            );

You have to use http_build_query() prior to sending POST data (array) to cURL.

string http_build_query ( mixed $query_data [, string $numeric_prefix [, string $arg_separator [, int $enc_type = PHP_QUERY_RFC1738 ]]] )

So your $postData should look like this:

$postData = http_build_query(
                                array(
                                'output_info' => 'compiled_code', 
                                'output_format' => 'text', 
                                'compilation_level' => 'SIMPLE_OPTIMIZATIONS', 
                                'js_code' => urlencode($content)
                                )
                            );
风启觞 2025-01-05 00:57:15

看起来 Google 的 API 不支持 multipart/form-data 数据。这对我来说似乎有点蹩脚......

根据关于curl_setopt()的PHP文档

将数组传递给 CURLOPT_POSTFIELDS 会将数据编码为 multipart/form-data,
传递 URL 编码字符串时会将数据编码为 application/x-www-form-urlencoded。

因此,如果将代码的第 4 行更改为如下所示,它应该可以工作:

$postData = 'output_info=compiled_code&output_format=text&compilation_level=SIMPLE_OPTIMIZATIONS&js_code=' . urlencode($content);

换句话说,您必须自己进行 URL 编码 - 显然您不能依赖 cURL 来获取数组并为您进行编码。

Looks like Google's API doesn't support multipart/form-data data. Which seems a bit lame to me...

According to the PHP documentation on curl_setopt():

Passing an array to CURLOPT_POSTFIELDS will encode the data as multipart/form-data,
while passing a URL-encoded string will encode the data as application/x-www-form-urlencoded.

So it should work if you change the 4th line of your code to something like this:

$postData = 'output_info=compiled_code&output_format=text&compilation_level=SIMPLE_OPTIMIZATIONS&js_code=' . urlencode($content);

In other words, you have to do the URL encoding yourself - you apparently can't rely on cURL to take an array and encode it for you.

朦胧时间 2025-01-05 00:57:15

1.) 不要使用 array() 以避免切换到 multipart/form-data

将数组传递给 CURLOPT_POSTFIELDS 会将数据编码为
multipart/form-data,同时传递 URL 编码的字符串时将进行编码
数据为 application/x-www-form-urlencoded。

2.)不要使用http_build_query()来避免双重编码问题(@Wesley)。此外,您不能两次使用密钥(我知道这个解决方法,但它很难看)。

我使用 http_build_query() 时出现的一些错误消息:

JSC_PARSE_ERROR 输入_0
解析错误。非法八进制数字 9;将其解释为十进制数字
解析错误。语法错误
错误 - 解析错误。函数参数之前缺少 (

3 之前。)我的建议:

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Expect:',
    'Content-type: application/x-www-form-urlencoded',
));
curl_setopt($ch, CURLOPT_TIMEOUT, 30); // don't set it to low! sending and response needs time
curl_setopt($ch, CURLOPT_ENCODING, ''); // automatically sets supported encodings
//curl_setopt($ch, CURLOPT_HEADER, true); // for debugging response header
//curl_setopt($ch, CURLINFO_HEADER_OUT, true); // for debugging request header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // false would echo the answer
curl_setopt($ch, CURLOPT_POST, true);
// settings
curl_setopt($ch, CURLOPT_POSTFIELDS,
    'output_format=json'
    .'&output_info=compiled_code'
    .'&output_info=warnings'
    .'&output_info=errors'
    .'&output_info=statistics'
    .'&compilation_level=ADVANCED_OPTIMIZATIONS'
    .'&warning_level=verbose'
    //.'&output_file_name=default.js'
    //.'&code_url='
    .'&js_code=' . urlencode($js_code)
);
curl_setopt($ch, CURLOPT_URL, 'http://closure-compiler.appspot.com/compile');
$response = curl_exec($ch);
//$response = curl_getinfo($ch, CURLINFO_HEADER_OUT) . $response; // for debugging request header
print_r(json_decode($response, true));

1.) Don't use an array() to avoid switch to multipart/form-data:

Passing an array to CURLOPT_POSTFIELDS will encode the data as
multipart/form-data, while passing a URL-encoded string will encode
the data as application/x-www-form-urlencoded.

2.) Don't use http_build_query() to avoid double encoding problems (@Wesley). In addition you aren't able to use keys twice (I know this workaround, but its ugly).

Some of my error messages by using http_build_query():

JSC_PARSE_ERROR Input_0
Parse error. illegal octal literal digit 9; interpreting it as a decimal digit
Parse error. syntax error
ERROR - Parse error. missing ( before function parameters

3.) My proposal:

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Expect:',
    'Content-type: application/x-www-form-urlencoded',
));
curl_setopt($ch, CURLOPT_TIMEOUT, 30); // don't set it to low! sending and response needs time
curl_setopt($ch, CURLOPT_ENCODING, ''); // automatically sets supported encodings
//curl_setopt($ch, CURLOPT_HEADER, true); // for debugging response header
//curl_setopt($ch, CURLINFO_HEADER_OUT, true); // for debugging request header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // false would echo the answer
curl_setopt($ch, CURLOPT_POST, true);
// settings
curl_setopt($ch, CURLOPT_POSTFIELDS,
    'output_format=json'
    .'&output_info=compiled_code'
    .'&output_info=warnings'
    .'&output_info=errors'
    .'&output_info=statistics'
    .'&compilation_level=ADVANCED_OPTIMIZATIONS'
    .'&warning_level=verbose'
    //.'&output_file_name=default.js'
    //.'&code_url='
    .'&js_code=' . urlencode($js_code)
);
curl_setopt($ch, CURLOPT_URL, 'http://closure-compiler.appspot.com/compile');
$response = curl_exec($ch);
//$response = curl_getinfo($ch, CURLINFO_HEADER_OUT) . $response; // for debugging request header
print_r(json_decode($response, true));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文