PHP三种POST方法:
function send_post($url, $post_data) {
$postdata = http_build_query($post_data);
$options = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type:application/x-www-form-urlencoded',
// 'header' => 'Content-type:application/json',
'content' => $postdata,
'timeout' => 15 * 60 // 超时时间(单位:s)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
return $result;
}
function http_post_data($url, $data_string) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// curl_setopt($ch, CURLOPT_HTTPHEADER, array(
// 'Content-Type: application/json',
// // 'Content-Type: application/x-www-form-urlencoded',
// 'Content-Length: ' . strlen($data_string))
// );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
return $result;
}
/**
* 给定url,发送post/get请求,返回服务器json字符串
* @param String $url url地址
* @param String $data post给服务器参数
* @return String $result json格式的字符串
*/
function httpsRequest($url, $data = null){
$ch = curl_init();
//设置url
curl_setopt($ch, CURLOPT_URL, $url);
//设置curl_exec方法的返回值,结果返回,不自动输出任何内容
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if(!empty($data)){
//发送post请求,
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
//跳过ssl检查项
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//发送请求
$result = curl_exec($ch);
curl_close($ch);
return $result;
}