1.curl实现
/*通过curl发送post数据*/
function execUpload($post_data, $url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, false);
//启用时会发送一个常规的POST请求,类型为:application/x-www-form-urlencoded,就像表单提交的一样。
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_BINARYTRANSFER,true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$info= curl_exec($ch);
curl_close($ch);
return $info;
}
CURLOPT_RETURNTRANSFER 设为true 不直接在页面输出内容
2.file_get_contents实现
/*通过file_get_contents发送post数据*/
function postData($post_string,$url){
$context = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded' .'\r\n'.
'User-Agent : Jimmy\'s POST Example beta' .'\r\n'.
'Content-length:' . strlen($post_string) + 8,
'content' => $post_string)
);
$stream_context = stream_context_create($context);
$data = file_get_contents($url, false, $stream_context);
}