php中http_build_query 函数用法详解
在php中http_build_query函数是一个被大多数据程序员看忘记的函数,就我现在都不知道http_build_query的作用,下面我给大家分享一篇文章一起来学习学习吧.
具体方法:
当我们使用CURL来post数据的时候,需要设置post的数据curl_setopt($c, CURLOPT_POSTFIELDS, $post_data);假如这里的$data是
$data = array ( 'name' => 'www.111cn.net' , 'time' => '2012-2-3' )接下来,需要先将$data变成字符串
$post_data = http_build_query( $data );而采用 http_build_query 转换后再curl_setopt($c, CURLOPT_POSTFIELDS, $post_data);
看起来没有什么问题.但在实际操作中,$post_data 并没有被post过去.于是,自己写了个转换的方法后就OK了.
实例代码如下:
function getStr( $array , $Separator = '&' ) { if ( empty empty ( $array )) return ; if (! is_array ( $array )) { return $array ; } $returnStr = '' ; foreach ( $array as $key => $val ) { $temp = '' ; if ( is_array ( $val )) { for ( $i = 0; $i < count ( $val ); $i ++) { $returnStr .= $key . '[' . $i . ']' . '=' . $val [ $i ] . $Separator ; } } else { $returnStr .= $key . '=' . $val . $Separator ; } } $returnStr = substr (trim( $returnStr ), 0, -1); return $returnStr ; }测试 http_build_query($data,"","&"); 即可,无需自己写方法解析了.http_build_query 就是将一个数组转换成url ?后面的参数字符串,会自动进行urlencode处理,string http_build_query ( array formdata [, string numeric_prefix]),后面的给数组中没有指定键或者键为数字的加下标.
查看更多关于php中http_build_query 函数用法详解 - php函数的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://haodehen.cn/did30623