beautiful goodbye A problem with http_build_query in php

WBOY
Release: 2016-07-29 08:48:22
Original
953 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 the 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);
Using http_build_query to convert and then
curl_setopt($c, CURLOPT_POSTFIELDS, $post_data);
There seems to be 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,"","&");. There is no need to write a method to parse it yourself.
http_build_query A remote attacker can exploit the vulnerability to obtain sensitive memory information. Please use it with caution

The above introduces a problem of http_build_query in beautiful goodbye php, including the content of beautiful goodbye. I hope it will be helpful to friends who are interested in PHP tutorials.

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