CURL 笔记

September 14, 2016 4:15 PM

POST

$url='';
$data=array('content'=>$xml); //需要请求的数据 
$curl=curl_init(); //初始化
curl_setopt($curl,CURLOPT_URL,$url); //POST的URL 
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1); //返回数据 
curl_setopt($curl,CURLOPT_POST,1);//post方式请求
curl_setopt($curl,CURLOPT_POSTFIELDS,$data); //请求的数据
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, FALSE); //HTTPS协议是使用
curl_setopt($curl,CURLOPT_SSL_VERIFYHOST, FALSE); //HTTPS协议是使用
$response=curl_exec($curl); //执行请求 
curl_close($curl); //关闭 
var_dump($response);

GET

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);// 要求结果为字符串且输出到屏幕上 
curl_setopt($ch, CURLOPT_HEADER, 0); // 不要http header 加快效率
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)'); 
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, FALSE); //HTTPS协议是使用
curl_setopt($curl,CURLOPT_SSL_VERIFYHOST, FALSE); //HTTPS协议是使用
curl_setopt($ch, CURLOPT_TIMEOUT, 15); 
$output = curl_exec($ch);
curl_close($ch);
var_dump($output);