/**
*File:curl.class.php
*Createdon:2014-8-26,8:34:01
*copyright小皓(C)2013-2099版权所有
*www.haosblog.com
*
*CURL封装类,本类大部分操作均支持链式操作
*
*example:
*
*$curl=newCurl($url);
*$curl->exec();
*//发送post数据
*$curl->post(array('username'=>'用户名'))->exec();
*/
classCurl{
private$ch;
private$flag_if_have_run=false;
private$has_cloase=true;
publicfunction__construct($url='',$forgeIP=false){
$this->init($url,$forgeIP);
}
/**
*初始化CURL。如果CURL未被关闭,则先关闭
*
*@paramtype$url
*@return\Common\Library\Curl
*/
publicfunctioninit($url='',$forgeIP=false){
if(!$this->has_cloase){//如果上一次连接尚未结束,则先关闭
$this->close();
}
if($forgeIP){//伪造IP,将IP伪造为访问者的IP
if(Validate::isIPAddress($forgeIP)){
$ip=$forgeIP;
}else{
$ip=$_SERVER['SERVER_ADDR'];
}
$this->set_ip($ip);
}
$this->ch=curl_init($url);
curl_setopt($this->ch,CURLOPT_RETURNTRANSFER,1);
$this->has_cloase=false;
return$this;
}
publicfunctionsetUrl($url){
curl_setopt($this->ch,CURLOPT_URL,$url);
return$this;
}
publicfunctionclose(){
if(!$this->has_close){
curl_close($this->ch);
$this->has_cloase=true;
}
}
publicfunction__destruct(){
$this->close();
}
/**
*设置页面超时时间,支持链式操作
*
*@paramtype$timeout
*@return\Common\Library\Curl
*/
publicfunctionset_time_out($timeout){
curl_setopt($this->ch,CURLOPT_TIMEOUT,intval($timeout));
return$this;
}
/**
*伪造来源路径
*
*@paramtype$referer
*@return\Common\Library\Curl
*/
publicfunctionset_referer($referer){
if(!empty($referer)){
curl_setopt($this->ch,CURLOPT_REFERER,$referer);
}
return$this;
}
/**
*设置cookie
*本方法仅发送cookie信息到远端,不保存远端返回的cookie信息
*
*@paramtype$cookie_file cookie文件的存储路径
*@return\Common\Library\Curl
*/
publicfunctionload_cookie($cookie_file){
$this->_checkCookie($cookie_file);
curl_setopt($this->ch,CURLOPT_COOKIEFILE,$cookie_file);
return$this;
}
/**
*设置cookie
*发送cookie到远端,并保存远端返回的cookie
*
*@paramtype$cookie_file
*@return\Common\Library\Curl
*/
publicfunctioncookie($cookie_file){
$this->_checkCookie($cookie_file);
curl_setopt($this->ch,CURLOPT_COOKIEFILE,$cookie_file);
curl_setopt($this->ch,CURLOPT_COOKIEJAR,$cookie_file);
return$this;
}
/**
*设置cookie
*本方法将不发送cookie信息,仅接收返回的cookie并保存
*
*@paramtype$cookie_file
*@return\Common\Library\Curl
*/
publicfunctionsave_cookie($cookie_file=""){
//设置缓存文件,例如a.txt
if(empty($cookie_file)){
$cookie_file=tempnam('./','cookie');
}
$this->_checkCookie($cookie_file);
curl_setopt($this->ch,CURLOPT_COOKIEJAR,$cookie_file);
return$this;
}
privatefunction_checkCookie($cookie_file){
if(!\Think\Storage::has($cookie_file)){
\Think\Storage::put($cookie_file,'');
}
}
/**
*执行curl请求
*
*@returntype
*/
publicfunctionexec(){
$str=curl_exec($this->ch);
$this->flag_if_have_run=true;
return$str;
}
/**
*设置发送POST请求。没有调用过该方法默认使用GET方法提交
*
*@paramtype$postData post的数据,支持数组和“xxx=1&x=2”两种格式
*@return\Common\Library\Curl
*/
publicfunctionpost($postData){
curl_setopt($this->ch,CURLOPT_POST,1);
//echo($postQuery);die;
curl_setopt($this->ch,CURLOPT_POSTFIELDS,$postData);
return$this;
}
/**
*获取curl的信息
*
*@returntype
*@throwsException
*/
publicfunctionget_info(){
if($this->flag_if_have_run==true){
returncurl_getinfo($this->ch);
}else{
thrownewException(" 需先运行(执行exec),再获取信息");
}
}
/**
*设置代理
*
*@paramtype$proxy
*@return\Common\Library\Curl
*/
publicfunctionset_proxy($proxy){
//设置代理,例如'68.119.83.81:27977'
curl_setopt($this->ch,CURLOPT_PROXYTYPE,CURLPROXY_SOCKS5);
curl_setopt($this->ch,CURLOPT_PROXY,$proxy);
return$this;
}
/**
*设置请求的IP
*
*@paramtype$ip
*@returntype
*/
publicfunctionset_ip($ip=''){
if(!empty($ip)){
curl_setopt($this->ch,CURLOPT_HTTPHEADER,array("X-FORWARDED-FOR:$ip","CLIENT-IP:$ip"));
}
return$ip;
}
}
|