curl get:
1)直接输出
$ch=curl_init(); curl_setopt($ch,CURLOPT_URL,"http://testopen.api.yaolan.com/api/user/reg"); curl_exec($ch); curl_close($ch);
2)curl_get函数
function curl_get($url){ $ch=curl_init(); curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); $content=curl_exec($ch); curl_close($ch); return $content; }
curl_post 函数:
function curl_post($url,$data=array()){ $ch=curl_init(); curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); curl_setopt($ch,CURLOPT_POST,true);//如果有下面的一行代码,这个可以不设置 curl_setopt($ch,CURLOPT_POSTFIELDS,$data); $content=curl_exec($ch); curl_close($ch); return $content; } $data=array('uname'=>'zdctest','email'=>'zdc@yaolan.com');
//$data=array('uname'=>'zdctest','email'=>'zdc@yaolan.com','file_name' => '@/data/lnmp/autoreg/logs/log20150415.txt');//上传文件需要加@符号 php 5.6 之后要加 curl_setopt($ch, CURLOPT_SAFE_UPLOAD,false);才能上传成功
$url="http://testopen.api.yaolan.com/api/user/reg"; echo curl_post($url,$data);
携带header post
public static function curlPost($url,$data=array(),$header=array()){ $ch=curl_init(); curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); curl_setopt($ch,CURLOPT_POST,true);//如果有下面的一行代码,这个可以不设置 curl_setopt($ch,CURLOPT_POSTFIELDS,$data); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); empty($header) or curl_setopt($ch, CURLOPT_HTTPHEADER, $header); $content=curl_exec($ch); curl_close($ch); return $content; }
-----------------------------------------新版
curl:request
public static function curlRequest($url,$method='post',$data=array(),$header=array()){ $ch = curl_init(); //初始化CURL句柄 curl_setopt($ch, CURLOPT_URL, $url); //设置请求的URL curl_setopt($ch, CURLOPT_RETURNTRANSFER,true); //设为TRUE把curl_exec()结果转化为字串,而不是直接输出 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); //设置请求方式 empty($data) or curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//设置提交的字符串 empty($header) or curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//设置提交header 0===strpos($url,'https:') and curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//https请求加这个代码 $content=curl_exec($ch); curl_close($ch); return $content; }
curl post get
//post 函数 function curl_post($url,$data=array(),array $header=array()){ $ch=curl_init(); curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); curl_setopt($ch,CURLOPT_POST,true);//如果有下面的一行代码,这个可以不设置 curl_setopt($ch,CURLOPT_POSTFIELDS,$data); 0===strpos($url,'https:') and curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//https请求加这个代码 empty($header) or curl_setopt($ch, CURLOPT_HTTPHEADER, $header); $content=curl_exec($ch); curl_close($ch); return $content; } //get函数携带 header function curl_get($url,array $header=array()){ $ch=curl_init(); curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); 0===strpos($url,'https:') and curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//https请求加这个代码 empty($header) or curl_setopt($ch, CURLOPT_HTTPHEADER, $header); $content=curl_exec($ch); curl_close($ch); return $content; }
curlRequest:(new)
private function curlRequest($url,$method='post',$data=array(),$header=array()){ $ch = curl_init(); //初始化CURL句柄 curl_setopt($ch, CURLOPT_URL, $url); //设置请求的URL curl_setopt($ch, CURLOPT_RETURNTRANSFER,true); //设为TRUE把curl_exec()结果转化为字串,而不是直接输出 if('post'!=strtolower($method)){ curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); //设置请求方式 }else{ curl_setopt($ch,CURLOPT_POST,true); } is_array($data) and $data=http_build_query($data); empty($data) or curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//设置提交的字符串 empty($header) or curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//设置提交header 0===strpos($url,'https:') and curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//https请求加这个代码 $content=curl_exec($ch); curl_close($ch); return $content; }