PHP curl 的CURLOPT_POSTFIELDS之数组和字符串之谜

现象

在最近的工作中遇到一个问题,就是使用post发送请求,post数据死活传递过不去,一直是请求返回error。 
代码如下:

$post = array(
    'userid' => 1000034443,
);
function curlPost($url, $headers, $post){
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,$url);
    if(!empty($headers)){
        curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
    }
    curl_setopt($ch, CURLOPT_POST, 1);//设置为POST方式
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);//POST数据
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); //如果成功只将结果返回,不自动输出任何内容。
    curl_setopt($ch, CURLOPT_HEADER,0);//如果想把一个头包含在输出中,设置这个选项为一个非零值。
    curl_setopt($ch, CURLINFO_HEADER_OUT,1);//启用时追踪句柄的请求字符串。
    $json = curl_exec($ch);
    $headers = curl_getinfo($ch, CURLINFO_HEADER_OUT);
    echo "\n\n=====请求返回=====\n";
    echo "out headers:\t".$headers ."\n";
    $hearLen = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
    echo "header len:\t".$hearLen ."\n";
    $statuscode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    echo "httpcode:\t".$statuscode."\n";
    echo "\n===================\n";
    return $json;
    @curl_close($ch);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

发现什么问题吗? 
结果如下: 
这里写图片描述 
感觉没什么问题啊?怎么会传不过去呢?然后就凭着感觉修改了代码

function curlPost($url, $headers, $post){
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,$url);
    if(!empty($headers)){
        curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
    }
    curl_setopt($ch, CURLOPT_POST, 1);//设置为POST方式
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));//POST数据
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); //如果成功只将结果返回,不自动输出任何内容。
    curl_setopt($ch, CURLOPT_HEADER,0);//如果想把一个头包含在输出中,设置这个选项为一个非零值。
    curl_setopt($ch, CURLINFO_HEADER_OUT,1);//启用时追踪句柄的请求字符串。
    $json = curl_exec($ch);
    $headers = curl_getinfo($ch, CURLINFO_HEADER_OUT);
    echo "\n\n=====请求返回=====\n";
    echo "out headers:\t".$headers ."\n";
    $hearLen = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
    echo "header len:\t".$hearLen ."\n";
    $statuscode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    echo "httpcode:\t".$statuscode."\n";
    echo "\n===================\n";
    return $json;
    @curl_close($ch);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

哪里不一样了呢? 
看看效果先: 
这里写图片描述 
对比发现,content type值不一样了!!!

怎么回事呢?

当CURLOPT_POSTFIELDS被设置为数组时,HTTP头会发送Content_type: application/x-www-form-urlencoded,这个是正常的网页提交表单时,浏览器发送的头部,而multipart/form-data我们知道这是用于上传文件的表单,包括了boundary分界符,会多出很多字节.

手册上提到:

The full data to post in a HTTP “POST” operation. To post a file, prepend a filename with @ and use the full path. This can either be passed as a urlencoded string like ‘para1=val1&para2=val2&…’ or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data.

使用数组提供post数据时,默认把content_type设为了multipart/form-data,虽然对于大多数web服务器并没有影响,但是还是有少部分服务器不兼容.

结论

这次我的没有返回multipart/form-data,但确实是这个问题,建议post数据可以使用http_build_query()函数

猜你喜欢

转载自my.oschina.net/yonghan/blog/1629893