0

0

curl类封装

大家讲道理

大家讲道理

发布时间:2016-11-08 13:26:47

|

1569人浏览过

|

来源于php中文网

原创

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("

需先运行( 执行exec ),再获取信息

"); } } ?>

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
Golang gRPC 服务开发与Protobuf实战
Golang gRPC 服务开发与Protobuf实战

本专题系统讲解 Golang 在 gRPC 服务开发中的完整实践,涵盖 Protobuf 定义与代码生成、gRPC 服务端与客户端实现、流式 RPC(Unary/Server/Client/Bidirectional)、错误处理、拦截器、中间件以及与 HTTP/REST 的对接方案。通过实际案例,帮助学习者掌握 使用 Go 构建高性能、强类型、可扩展的 RPC 服务体系,适用于微服务与内部系统通信场景。

8

2026.01.15

公务员递补名单公布时间 公务员递补要求
公务员递补名单公布时间 公务员递补要求

公务员递补名单公布时间不固定,通常在面试前,由招录单位(如国家知识产权局、海关等)发布,依据是原入围考生放弃资格,会按笔试成绩从高到低递补,递补考生需按公告要求限时确认并提交材料,及时参加面试/体检等后续环节。要求核心是按招录单位公告及时响应、提交材料(确认书、资格复审材料)并准时参加面试。

44

2026.01.15

公务员调剂条件 2026调剂公告时间
公务员调剂条件 2026调剂公告时间

(一)符合拟调剂职位所要求的资格条件。 (二)公共科目笔试成绩同时达到拟调剂职位和原报考职位的合格分数线,且考试类别相同。 拟调剂职位设置了专业科目笔试条件的,专业科目笔试成绩还须同时达到合格分数线,且考试类别相同。 (三)未进入原报考职位面试人员名单。

58

2026.01.15

国考成绩查询入口 国考分数公布时间2026
国考成绩查询入口 国考分数公布时间2026

笔试成绩查询入口已开通,考生可登录国家公务员局中央机关及其直属机构2026年度考试录用公务员专题网站http://bm.scs.gov.cn/pp/gkweb/core/web/ui/business/examResult/written_result.html,查询笔试成绩和合格分数线,点击“笔试成绩查询”按钮,凭借身份证及准考证进行查询。

11

2026.01.15

Java 桌面应用开发(JavaFX 实战)
Java 桌面应用开发(JavaFX 实战)

本专题系统讲解 Java 在桌面应用开发领域的实战应用,重点围绕 JavaFX 框架,涵盖界面布局、控件使用、事件处理、FXML、样式美化(CSS)、多线程与UI响应优化,以及桌面应用的打包与发布。通过完整示例项目,帮助学习者掌握 使用 Java 构建现代化、跨平台桌面应用程序的核心能力。

65

2026.01.14

php与html混编教程大全
php与html混编教程大全

本专题整合了php和html混编相关教程,阅读专题下面的文章了解更多详细内容。

36

2026.01.13

PHP 高性能
PHP 高性能

本专题整合了PHP高性能相关教程大全,阅读专题下面的文章了解更多详细内容。

75

2026.01.13

MySQL数据库报错常见问题及解决方法大全
MySQL数据库报错常见问题及解决方法大全

本专题整合了MySQL数据库报错常见问题及解决方法,阅读专题下面的文章了解更多详细内容。

21

2026.01.13

PHP 文件上传
PHP 文件上传

本专题整合了PHP实现文件上传相关教程,阅读专题下面的文章了解更多详细内容。

35

2026.01.13

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Git 教程
Git 教程

共21课时 | 2.7万人学习

Kotlin 教程
Kotlin 教程

共23课时 | 2.5万人学习

PHP新手语法线上课程教学
PHP新手语法线上课程教学

共13课时 | 0.9万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号