场景
. curl请求API之前,使用http_build_query将post数据转成url encode字符串
然后发送post数据;
这时候如果api规定必须传递的参数如果是空的话 则http_build_query会将这个参数filter掉
. api方的确是没有获得想要的数据 ...
分析
. 慎用
示例
protected function curlRequestForPost($url, $post_data)
{
if (is_array($post_data)) {
$post_data = http_build_query($post_data);
}
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($curl, CURLOPT_TIMEOUT, 20);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
$list_stat = curl_exec($curl);
if (curl_errno($curl)) {
return [
'success' => false,
'msg' => curl_error($curl)
];
}
curl_close($curl);
return $list_stat;
}