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

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了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()

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

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

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

脚本宝典总结

以上是脚本宝典为你收集整理的php – Google Closure编译器和multipart / form-data无法正常工作全部内容,希望文章能够帮你解决php – Google Closure编译器和multipart / form-data无法正常工作所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。