微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – Google Closure编译器和multipart / form-data无法正常工作

我正在向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'));

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

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

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

谢谢,
韦斯利

解决方法

看起来Google的API不支持多部分/表单数据数据.这对我来说似乎有点蹩脚……

根据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.

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

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐