Home > Backend Development > PHP Tutorial > php中http_build_query 的一个问题_PHP

php中http_build_query 的一个问题_PHP

WBOY
Release: 2016-06-01 12:13:33
Original
1033 people have browsed it

当我们使用CURL来post数据的时候,需要设置post的数据
curl_setopt($c, CURLOPT_POSTFIELDS, $post_data);

假如这里的$data是
复制代码 代码如下:
$data = array(
'name'=>'scofield',
'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($array))
return;
if (!is_array($array)) {
return $array;
}
$returnStr = '';
foreach ($array as $key => $val) {
$temp = '';
if (is_array($val)) {
for ($i = 0; $i $returnStr .= $key . '[' . $i . ']' . '=' . $val[$i] . $Separator;
}
} else {
$returnStr.= $key . '=' . $val . $Separator;
}
}
$returnStr = substr(trim($returnStr), 0, -1);
return $returnStr;
}

感谢 黄斌-huangbin 童鞋的测试 http_build_query($data,"","&"); 即可,无需自己写方法解析了。

http_build_query 远程攻击者可以利用漏洞获得敏感内存信息。请大家谨慎使用

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template