搜尋
curl类封装 May 23, 2016 am 08:39 AM
php

[PHP]代码   

<?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[&#39;HTTP_USER_AGENT&#39;]; // 模拟用户使用的浏览器   
        $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[&#39;curl&#39;])){
		        curl_setopt($this->ch,CURLOPT_COOKIE,$_COOKIE[&#39;curl&#39;]);
		    }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(&#39;curl_cookie&#39;);
            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(&#39;curl&#39;,$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(&#39;curl_cookie&#39;);
            if($ret){
                $ret = $kv->set(&#39;curl_cookie&#39;, $cookie_val );
                
            }else{
                 $ret = $kv->add(&#39;curl_cookie&#39;, $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;  
    }  
    //设置代理 ,例如&#39;68.119.83.81:27977&#39;  
    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 &#39;Errno&#39;.curl_error($this->ch);  
            return false;			
        } 
        if($this->show_header == 1){ //数组形式返回头信息和body信息 
		    list($header, $body) = explode("\r\n\r\n", $res);
			$arr[&#39;header&#39;] = $header;
			$arr[&#39;body&#39;] = $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(&#39;;&#39;,array_unique(explode(&#39;;&#39;,implode(&#39;;&#39;,$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>");  
    }  
	 
}  
?>

                   

                   

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前By尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
1 個月前By尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
4 週前By尊渡假赌尊渡假赌尊渡假赌

熱工具

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)