Google Closure Compiler和multipart / form-data不工作 [英] Google Closure Compiler and multipart/form-data not working
问题描述
我向google closure编译器API服务发出请求:
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'));
但是请求失败,我从google收到此错误消息:
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.
我查看了头文件,并且发送了Content-Type头文件:
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?
谢谢您,
Wesley
Thank you, Wesley
推荐答案
Google的API不支持多部分/表单数据数据。这似乎对我有点跛脚...
Looks like Google's API doesn't support multipart/form-data data. Which seems a bit lame to me...
将数组传递给CURLOPT_POSTFIELDS会将数据编码为multipart / form-data,
当传递一个URL编码的字符串将编码数据为application / x-www-form-urlencoded。
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);
换句话说,你必须自己做URL编码 - 你显然不能依赖cURL以获取数组并为您编码。
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.
这篇关于Google Closure Compiler和multipart / form-data不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!