用http_build_query()函数在curl处理post请求参数

来源:互联网 发布:mac图片压缩软件 编辑:程序博客网 时间:2024/06/05 07:16

当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服务器并没有影响,但是还是有少部分服务器不兼容.

建议post数据可以使用http_build_query()函数处理,获取url形式字符串.

http_build_query

(PHP 5)

http_build_query -- 生成 url-encoded 之后的请求字符串描述string http_build_query ( array formdata [, string numeric_prefix] )

使用给出的关联(或下标)数组生成一个 url-encoded 请求字符串。参数 formdata 可以是数组或包含属性的对象。一个 formdata 数组可以是简单的一维结构,也可以是由数组组成的数组(其依次可以包含其它数组)。如果在基础数组中使用了数字下标同时给出了 numeric_prefix 参数,此参数值将会作为基础数组中的数字下标元素的前缀。这是为了让 PHP 或其它 CGI 程序在稍后对数据进行解码时获取合法的变量名。

例子 1. http_build_query() 使用示例

<?php 
$data = array('foo'=>'bar', 
              'baz'=>'boom', 
              'cow'=>'milk', 
              'php'=>'hypertext processor'); 
echo http_build_query($data); 
/* 输出: 
       foo=bar&baz=boom&cow=milk&php=hypertext+processor 
*/ 

?>

例子 2. http_build_query() 使用数字下标的元素

<?php 
$data = array('foo', 'bar', 'baz', 'boom', 'cow' => 'milk', 'php' =>'hypertext processor'); 
echo http_build_query($data); 
/* 输出: 
       0=foo&1=bar&2=baz&3=boom&cow=milk&php=hypertext+processor 
*/ 
echo http_build_query($data, 'myvar_'); 
/* 输出: 
       myvar_0=foo&myvar_1=bar&myvar_2=baz&myvar_3=boom&cow=milk&php=hypertext+processor 
*/ 
?>

例子 3. http_build_query() 使用复杂的数组

<?php 
$data = array('user'=>array('name'=>'Bob Smith', 
                            'age'=>47, 
                            'sex'=>'M', 
                            'dob'=>'5/12/1956'), 
              'pastimes'=>array('golf', 'opera', 'poker', 'rap'), 
              'children'=>array('bobby'=>array('age'=>12, 
                                               'sex'=>'M'), 
                                'sally'=>array('age'=>8, 
                                               'sex'=>'F')), 
              'CEO'); 
echo http_build_query($data, 'flags_'); 
/* 输出:(为了可读性对其进行了折行) 
       user[name]=Bob+Smith&user[age]=47&user[sex]=M&user[dob]=5%1F12%1F1956& 
       pastimes[0]=golf&pastimes[1]=opera&pastimes[2]=poker&pastimes[3]=rap& 
       children[bobby][age]=12&children[bobby][sex]=M&children[sally][age]=8& 
       children[sally][sex]=F&flags_0=CEO 
    注意:只有基础数组中的数字下标元素“CEO”才获取了前缀,其它数字下标元素(如 
    pastimes 下的元素)则不需要为了合法的变量名而加上前缀。 
*/ 
?>

例子 4. http_build_query() 使用对象

<?php 
class myClass { 
   var $foo; 
   var $baz; 
   function myClass() { 
    $this->foo = 'bar'; 
    $this->baz = 'boom'; 
   } 

$data = new myClass(); 
echo http_build_query($data); 
/* 输出: 
       foo=bar&baz=boom 
*/ 
?>


参考文章:http://blog.csdn.net/havedream_one/article/details/52585331

原创粉丝点击