码迷,mamicode.com
首页 > 其他好文 > 详细

一个基础的CURL类

时间:2014-08-28 12:47:19      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   for   ar   div   

/**
 * 一个基础的CURL类
 *
 * @author Smala
 */
    class curl{
        public $ch;
        public $cookie = ‘/cookie‘;            
        public $rstr;                                
        public $info;                                
        
        public function __construct($ssl=true,$cookieName="tmp.cookie"){
         
            
            $this -> cookie = dirname(__FILE__)."/".$cookieName;
            $this -> ch = curl_init();
            curl_setopt($this -> ch ,CURLOPT_USERAGENT,‘Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36‘);//设置用户代理
            curl_setopt($this -> ch,CURLOPT_COOKIEJAR,$this -> cookie);        
            curl_setopt($this -> ch,CURLOPT_COOKIEFILE,$this -> cookie);    
            
            if($ssl){
                curl_setopt($this -> ch, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($this -> ch, CURLOPT_SSL_VERIFYHOST, false);
            }
            curl_setopt($this -> ch,CURLOPT_RETURNTRANSFER,1);
        }
        
        public function set($opt){
            foreach($opt as $key => $value)
            curl_setopt($this -> ch,$key,$value);
            return $this;
        }
        public function get($url,$data=array()){
            $queryString = http_build_query($data);
            if(!empty($queryString)){
                $url.= ‘?‘.$queryString;
            }
            curl_setopt($this -> ch,CURLOPT_URL,$url);
            return $this;
            
        }
        public function setHeader($data=array()){
            curl_setopt($this -> ch,CURLOPT_HTTPHEADER,$data);
            return $this;
        }
        public function post($url,$data = array()){
            $post = ‘‘;
            foreach($data as $key=>$value){
                $post .= $key.‘=‘.$value."&";
                
            }
            $post = trim($post,‘&‘);
            curl_setopt($this -> ch,CURLOPT_URL,$url);
            curl_setopt($this -> ch,CURLOPT_POST,1);        
            curl_setopt($this -> ch,CURLOPT_POSTFIELDS,$post);    
            return $this;
        }
        public function execute($close=true){
            
            $this -> rstr = curl_exec($this -> ch);
            $this -> info = curl_getinfo($this -> ch);
            if($close){
               // curl_close($this -> ch);
            }
            return $this -> rstr;
            
        }
        public function close(){
            curl_close($this -> ch);
        }
        
    }

 

一个基础的CURL类

标签:style   blog   http   color   os   io   for   ar   div   

原文地址:http://www.cnblogs.com/phpfensi/p/3941088.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!