curl类封装

<?php
/**
  * @author askwei
**/
 
class CURL  
{ 
    private $ch; 
    private $url = "http://www.baidu.com";
    private $flag_if_have_run;   //标记exec是否已经运行
    private $set_time_out = 20;  //设置curl超时时间
    private $cookie_file = "";  //cookie_file路径
    private $cookie_mode = 0;    //cookie保存模式 0不使用 1客户端、2服务器文件
    private $show_header = 0;    //是否输出返回头信息
    private $set_useragent = ""; //模拟用户使用的浏览器,默认为模拟
     
    //构造函数 
    public function __construct($url = ""){ 
        $this->ch = curl_init(); 
        $this->url = $url ? $url : $this->url;
        //$this->set_useragent = $_SERVER[‘HTTP_USER_AGENT‘]; // 模拟用户使用的浏览器  
        $this->set_useragent ="Mozilla/5.0 (iPhone; CPU iPhone OS 6_1_4 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/7.0 Mobile/10B350 Safari/9537.53";
        // $this->set_useragent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36";
        //$this->cookie_file=dirname(__FILE__)."/cookie_".md5(basename(__FILE__)).".txt";    //初始化cookie文件路径
        //$this->cookie_file= SAE_TMP_PATH.TmpFS;
        $this->cookie_file = "saekv://cookie_2014.txt";
    } 
    //关闭curl
    public function close(){ 
        curl_close($this->ch); 
    } 
    //析构函数 
    public function __destruct(){ 
        $this->close(); 
    } 
     
   //设置超时  
    public function set_time_out($timeout=20){ 
        if(intval($timeout) != 0)      
        $this->set_time_out = $timeout;
        return $this;
    } 
    //设置来源页面 
    public function set_referer($referer = ""){ 
        if (!empty($referer)) 
            curl_setopt($this->ch, CURLOPT_REFERER , $referer); 
        return $this; 
    }
    //设置cookie存放模式 1客户端、2服务器文件 
    public function set_cookie_mode($mode = ""){ 
        $this->cookie_mode = $mode;
        return $this;
    }
    //载入cookie 
    public function load_cookie(){ 
     
        if($this->cookie_mode == 1 ) {
            if(isset($_COOKIE[‘curl‘])){
                curl_setopt($this->ch,CURLOPT_COOKIE,$_COOKIE[‘curl‘]);
            }else{
                $this->exec();
                curl_setopt($this->ch,CURLOPT_COOKIE,$this->cookie_file);
            }
             
        }
        if($this->cookie_mode == 2 ) {
           
            curl_setopt($this->ch, CURLOPT_COOKIEFILE , $this->cookie_file);
             
        }
        if($this->cookie_mode == 3 ) {
            $kv = new SaeKV();
            $ret = $kv->init();
            $ret = $kv->get(‘curl_cookie‘);
            if($ret)
               curl_setopt($this->ch,CURLOPT_COOKIE, $ret);
             
        }
        return $this; 
    } 
    
    //设置保存cookie方式 $cookie_val 模式1为变量 模式2为文件路径
    public function save_cookie($cookie_val = "") { 
        //保存在客户端
        if($this->cookie_mode == 1 && $cookie_val){
           setcookie(‘curl‘,$cookie_val);
        }
        //保存服务器端
        if($this->cookie_mode == 2){
            if(!empty($cookie_val)) 
               $this->cookie_file =  $cookie_val;
            curl_setopt($this->ch, CURLOPT_COOKIEJAR , $this->cookie_file); 
        }
        //保存在sae
        if($this->cookie_mode == 3 && $cookie_val){
             $kv = new SaeKV();
             $ret = $kv->init();
             $ret = $kv->get(‘curl_cookie‘);
            if($ret){
                $ret = $kv->set(‘curl_cookie‘, $cookie_val );
                 
            }else{
                 $ret = $kv->add(‘curl_cookie‘, $cookie_val);
             
            }
        }
         
         
        return $this; 
         
    } 
    //post参数 (array) $post
    public function post ($post = ""){ 
        if($post && is_array($post)){
            curl_setopt($this->ch, CURLOPT_POST , 1); 
            curl_setopt($this->ch, CURLOPT_POSTFIELDS , $post ); 
        }
        return $this; 
    } 
    //设置代理 ,例如‘68.119.83.81:27977‘ 
    public function set_proxy($proxy = ""){
        if($proxy){
            curl_setopt($this->ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5); 
            curl_setopt($this->ch, CURLOPT_PROXY,$proxy);
        }          
        return $this; 
    } 
    //设置伪造ip 
    public function set_ip($ip=""){ 
        if(!empty($ip)) 
            curl_setopt($this->ch, CURLOPT_HTTPHEADER, array("X-FORWARDED-FOR:$ip", "CLIENT-IP:$ip")); 
        return $ip; 
    }
     //设置是否显示返回头信息
    public function show_header($show=0){
        $this->show_header = 0; 
        if($show)
            $this->show_header = 1;
        return $this; 
    }
 
     //设置请求头信息
    public function set_useragent($str=""){ 
        if($str) 
            $this->set_useragent = $str; 
        return $this; 
    }  
     
    //执行 
    public function exec ($url = ""){ 
        if(!$url) $url = $this->url;
        curl_setopt($this->ch, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查  
        curl_setopt($this->ch, CURLOPT_RETURNTRANSFER , 1 );    //获取的信息以文件流的形式返回    
        curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, 1); // 从证书中检查SSL加密算法是否存在
         curl_setopt($this->ch, CURLOPT_USERAGENT, $this->set_useragent); // 模拟用户使用的浏览器     
        curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转     
        curl_setopt($this->ch, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
        curl_setopt($this->ch, CURLOPT_TIMEOUT, $this->set_time_out);  //超时设置
        curl_setopt($this->ch, CURLOPT_HEADER, $this->show_header); // 显示返回的Header区域内容
        curl_setopt($this->ch, CURLOPT_NOBODY, 0);//不返回response body内容
         
        $res = curl_exec($this->ch);
        $this->flag_if_have_run = true;
        if (curl_errno($this->ch)) {     
            //echo ‘Errno‘.curl_error($this->ch); 
            return false;          
        }
        if($this->show_header == 1){ //数组形式返回头信息和body信息
            list($header, $body) = explode("\r\n\r\n", $res);
            $arr[‘header‘] = $header;
            $arr[‘body‘] = $body;
            if($this->cookie_mode == 1 || $this->cookie_mode == 3){ 
                preg_match_all("/set\-cookie:([^\r\n]*)/i", $header, $matches);
                //print_r($matches);
                if($matches && isset($matches[1]) ){
                    $val = implode(‘;‘,array_unique(explode(‘;‘,implode(‘;‘,$matches[1])))); //去重处理
                    if($val)
                      $this->save_cookie($val); //设置客户端保存cookie
                }
            }
            if($arr) return $arr;
        }
         
        return $res; 
    } 
     
     
    //返回  curl_getinfo信息
    public function get_info(){ 
        if($this->flag_if_have_run == true ) 
            return curl_getinfo($this->ch); 
        else  
            throw new Exception("<h1>需先运行( 执行exec ),再获取信息</h1>"); 
    } 
      
} 
?>
时间: 2024-10-05 05:21:38

curl类封装的相关文章

好用的curl类

<?php /*使用方法 $ch = new Curl_Class(); $ch->set_action("login", $loginurl, $refer); $postdata = array("username"=>"fortest", "password"=>"12345"); $ch->open()->get_cookie($this->_cookie)

QT 操作excel 类封装

1 # pro file 2 [plain] view plaincopy 3 CONFIG += qaxcontainer 4 5 QT += core 6 7 QT -= gui 8 9 TARGET = QExcel 10 CONFIG += console 11 CONFIG -= app_bundle 12 13 TEMPLATE = app 14 15 16 SOURCES += main.cpp \ 17 qexcel.cpp 18 19 HEADERS += \ 20 qexce

php---数据库类封装

为了节省以后的时间,今天封装了操作sql语句的一个类,在此保存起来,方面以后使用. 这个类的文件名:SqlTool.class.php 主要有dql和dml两个函数 看下面的源码" <?php class SqlTool{ private $conn; private $username="root"; private $password="1234"; private $host="127.0.0.1"; private $db

操作类封装

/*操作类封装 */ /*调用方法 如下: * var str= new IStrManipulation();//实例化字符串处理接口 * console.log(str.StrManipulation('StrManipulation',"111sss23123").getLength()); * var convert =new IConvert();//实例化类型转换接口 * console.log(convert.Convert('Convert',"1112312

MFC如和将类封装到DLL以及调用

MFC如和将类封装到DLL以及调用 分类: C++技术2012-06-27 17:40 1028人阅读 评论(0) 收藏 举报 dllfunmfcnullexe *1.先用mfc向导生成静态dll文件.*2.编辑增加类:*3.生成dll文件和lib文件:*4.将生成的dll和lib,和类的头文件复制到需要引用的文*件exe下:*///导出dll的头文件myClass.h#define DLLimport __declspec(dllimport)#define DLLexprot __decls

RUBY的类封装,继承,多态简单演示

class Person def initialize(name,age=18) @name=name @age=age @motherland="China" end def talk puts "my name is " [email protected]+",age is "+@age.to_s if @motherland == "China" puts "I am a China." else p

一个基础的CURL类

/** * 一个基础的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__)."/".$cookieNa

jQuery自定义类封装:

jQuery自定义类封装: (function ($) { $.DragField = function (arg) { var name = "你好";     //这个是私有变量,外部无法访问 this.testFun = function () {     //this.testFun方法,加上了this,就是公有方法了,外部可以访问. alert(arg.title + "," + name); }; }; })(jQuery); 使用方法: var a =

PHP通用CURL类

PHP通用CURL类,可POST/GET/文件传输 function do_curl($url, $params = array(), $upload = false, $type = 'POST') { $method = strtoupper($type); if ($method == 'GET') { $url = "{$url}?" . http_build_query($params); } $ch = curl_init(); // $header[] = "U