加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 22:09:18 所属栏目:PHP教程 来源:网络整理
导读:我正在向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_
我正在向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来获取数组并为您编码.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读