function request_post($url = "", $param = "", $header = "") {
if (empty($url) || empty($param)) {
return false;
}
$postUrl = $url;
$curlPost = $param;
$ch = curl_init(); // 初始化curl
curl_setopt($ch, CURLOPT_URL, $postUrl); // 抓取指定网页
curl_setopt($ch, CURLOPT_HEADER, 0); // 设置header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_POST, 1); // post提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($curlPost));
//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);//获取到重定向后的页面
if($header) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
}
// 增加 HTTP Header(头)里的字段
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
// 终止从服务端进行验证
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$data = curl_exec($ch); // 运行curl
curl_close($ch);
return $data;
}
如果遇见数据结构没问题,但是老是提醒Array to string conversion,给的数据不对的提示
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($curlPost));
数据发送前加http_build_query();