PHP CURL header 设置HOST主机头进行访问并 POST提交數據
$host = array("Host: act.qzone.qq.com");// 域名不帶http://
$data = array(
'aa' => 'xx',
'bb'=>'xx'
);
$url = 'http://127.0.0.1/xxx/xxx/api/';
var_dump( $this->curl_post($host, $data,$url) );
function curl_post($host,$data,$url)
{
$ch = curl_init();
$res= curl_setopt ($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_HTTPHEADER,$host);
$handles = curl_exec($ch);
curl_close($ch);
return $handles;
}
function postData($url, $post)
{
$ch = curl_init();
$timeout = 300;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //使用php curl获取页面内容或提交数据, 有时候希望返回的内容作为变量储存, 而不是直接输出
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);//在发起连接前等待的时间,如果设置为0,则无限等待。
$handles = curl_exec($ch);
curl_close($ch);
return $handles;
}
提交json 數據
function postData($url, $post)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type:application/json',
'Content-Length:'.strlen($post))
);
$handles = curl_exec($ch);
curl_close($ch);
return $handles;
}
刪除請求
$url='********************';
$this->postData($url);
private function postData($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "guest:unknow44");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, DELETE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type:application/json')
);
$handles = curl_exec($ch);
curl_close($ch);
return $handles;
}