Home > Backend Development > PHP Tutorial > A problem with http_build_query in php_PHP tutorial

A problem with http_build_query in php_PHP tutorial

WBOY
Release: 2016-07-21 15:19:40
Original
913 people have browsed it

When we use CURL to post data, we need to set the post data
curl_setopt($c, CURLOPT_POSTFIELDS, $post_data);

If the $data here is

Copy code The code is as follows:

$data = array(
'name'=>'scofield',
'time'=> '2012-2-3'
)

Next, you need to change $data into a string first
$post_data = http_build_query($data);
Use http_build_query After conversion,
curl_setopt($c, CURLOPT_POSTFIELDS, $post_data);

seems to have no problem. But in actual operation, $post_data is not posted. So, I wrote a conversion method and it was OK.
Copy code The code is as follows:

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 < count($ val); $i++) {
$returnStr .= $key . '[' . $i . ']' . '=' . $val[$i] . $Separator;
}
} else {
$returnStr.= $key . '=' . $val . $Separator;
}
}
$returnStr = substr(trim($returnStr), 0, -1);
return $returnStr;
}

Thanks to Huang Bin-huangbin for testing http_build_query($data,"","&"); , no need to write your own method to parse it. .

http_build_query A remote attacker could exploit this vulnerability to obtain sensitive memory information. Please use it with caution

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325209.htmlTechArticleWhen we use CURL to post data, we need to set the post data curl_setopt($c, CURLOPT_POSTFIELDS, $post_data ); If $data here is the copy code, the code is as follows: $data = arr...
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