封装curl类,post get方法实现网站请求
<?php
class RamDemo
{
//get方法
function RamGet($url,$arr)
{
if($arr!=''){
$ar=array();
foreach($arr as $k=>$v){
$ar[]=$k.'='.$v;
}
$url=$url.'?'.implode('&',$ar);
}
//return $url;die;
return $this->curl($url,$arr,'GET',false,false);
}
//post方法
function RamPost($url,$arr)
{
return $this->curl($url,$arr,'POST',false,false);
}
//curl方法
function curl($url,$data,$method,$setcooke=false,$cookie_file=false){
$ch = curl_init(); //1.初始化
curl_setopt($ch,CURLOPT_URL, $url); //2.请求地址
curl_setopt($ch,CURLOPT_CUSTOMREQUEST,$method);//3.请求方式
//4.参数如下
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
curl_setopt($ch,CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0'); //指定请求方式(浏览器)
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch,CURLOPT_AUTOREFERER,1);
if($method=="POST"){//5.post方式的时候添加数据
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
}
if($setcooke==true){
//把生成的cookie保存在指定的文件中
curl_setopt($ch, CURLOPT_COOKIEJAR,$cookie_file);
}else{
//直接从文件中读取cookie信息
curl_setopt($ch, CURLOPT_COOKIEFILE,$cookie_file);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$output = curl_exec($ch);
if (curl_errno($ch)) {
return curl_error($ch);
}
curl_close($ch);
return $output;
}
}
?>