php中http请求类的示例分析
admin
2023-06-13 13:23:01
0
  1. 代码:

    ]>
    * @property
    * 1、timeout    超时时间,默认5秒
    * 2、depth      重定向深度,默认3
    * 3、name       上传文件的名字,默认file
    * 4、cookie     模拟登录时cookie存储在本地的文件,默认cookie.txt
    * @method
    * 1、ssl        是否设置https           true:是  false:否
    * 2、auth       启用验证                user:用户名    pass:密码
    * 3、login      模拟登录,获取cookie
    * 4、cookie     使用cookie登录
    * 5、header     设置请求头              data:请求头数组
    * 6、proxy      设置服务器代理          url:代理服务器url   port:代理服务器端口
    * 7、agent      设置浏览器代理          browse:代理浏览器 默认:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)
    * 8、get        模拟get请求             data:传递的数据
    * 9、post       模拟post请求            data:传递的数据
    * 10、json      模拟json请求            data:传递的数据
    * 11、upload    模拟表单上传            files:上传的文件   array|string
    * 12、download  下载文件                dir:要下载的文件  格式:a/b
    * 13、run       执行                    ret:返回的数据
    * 14、info      获取执行信息            ret:返回的信息
    */
    class http{
    public $timeout = 5;  #  超时时间,默认5秒
    public $depth = 3;  #  重定向深度,默认3
    public $name = 'file';  #  上传文件的名字,默认file
    public $cookie = 'cookie.txt';  #  模拟登录时cookie存储在本地的文件,默认cookie_n
    private $scheme = '';
    private $host = '';
    private $path = '';
    private $query = '';
    private $options = array();
    private $ch;
    private $fp;
    private $progress = false;
    /*
     @desc:内部方法 设置get请求参数
     @param data 请求数据
     */
    private function setget($data){
        $scheme = $this->scheme;
        $host = $this->host;
        $path = $this->path;
        $query = $this->query;
        $sep = ($query || !empty($data))?"?":"";
        $qurl = $scheme.'://'.$host.$path.$sep.$query.$data;
        $this->options[CURLOPT_URL] = $qurl;
        return $this;
    }
    /*
     @desc:内部方法 设置post请求参数
     @param data 请求数据
     */
    private function setpost($data){
        $scheme = $this->scheme;
        $host = $this->host;
        $path = $this->path;
        $query = $this->query;
        $sep = $query?"?":"";
        $qurl = $scheme.'://'.$host.$path.$sep.$query;
        $this->options[CURLOPT_URL] = $qurl;
        $this->options[CURLOPT_POST] = 1;
        $this->options[CURLOPT_POSTFIELDS] = $data;
        return $this;
    }
    /*
     @desc:内部方法 设置最终请求参数
     */
    private function setopt(){
        $options = $this->options;
        curl_setopt_array(
                $this->ch,
                $options
            );
        return $this;
    }
    /*
     @desc:构造方法 设置初始请求参数
     @param url 请求地址
     */
    public function __construct($url){
        $info = parse_url($url);
        $this->scheme = $info['scheme']?:'http';
        $this->host = $info['host'];
        $this->path = $info['path'];
        $this->query = $info['query'];
        $this->ch = curl_init();
        $this->options[CURLOPT_CONNECTTIMEOUT] = $this->timeout;
        $this->options[CURLOPT_RETURNTRANSFER] = 1;
        $this->options[CURLOPT_FOLLOWLOCATION] = 1;
        $this->options[CURLINFO_HEADER_OUT] = true;
        $this->options[CURLOPT_ENCODING] = 'gzip';
        $this->options[CURLOPT_MAXREDIRS] = $this->depth;
    }
    /*
     @desc:是否设置https请求
     @param bool true:https请求 false:http请求
     */
    public function ssl($bool = false){
        if($bool){
            $this->scheme = 'https';
            $this->options[CURLOPT_SSL_VERIFYHOST] = 1;
            $this->options[CURLOPT_SSL_VERIFYPEER] = false;
        }
        return $this;
    }
    /*
     @desc:设置验证用户名、密码
     @param user 用户名
     @param pass 密码
     */
    public function auth($user,$pass){
        $this->options[CURLOPT_USERPWD] = $user.':'.$pass;
        return $this;
    }
    /*
     @desc:模拟登录
     */
    public function login(){
        $cookie = $this->cookie;
        $this->options[CURLOPT_COOKIEJAR] = $cookie;
        $this->options[CURLOPT_RETURNTRANSFER] = 0;
        return $this;
    }
    /*
     @desc:带cookie登录
     */
    public function cookie(){
        $cookie = $this->cookie;
        $this->options[CURLOPT_COOKIEFILE] = $cookie;
        return $this;
    }
    /*
     @desc:设置请求头信息
     @param data 请求头
     */
    public function header($data){
        $this->options[CURLOPT_HTTPHEADER] = $this->options[CURLOPT_HTTPHEADER]?:array();
        $this->options[CURLOPT_HTTPHEADER] = array_merge($this->options[CURLOPT_HTTPHEADER],$data);
        return $this;
    }
    /*
     @desc:设置代理服务器
     @param url 代理服务器url
     @param port 代理服务器端口
     */
    public function proxy($url,$port){
        $info = parse_url($url);
        $scheme = $info['scheme']?:'http';
        $host = $info['host'];
        $path = $info['path'];
        $purl = $scheme.'://'.$host.$path.':'.$port;
        $this->options[CURLOPT_PROXY] = $purl;
        return $this;
    }
    /*
     @desc:设置代理浏览器
     @param browse 代理浏览器
     */
    public function agent($browse = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)'){
        $this->options[CURLOPT_USERAGENT] = $browse;
        return $this;
    }
    /*
     @desc:模拟get请求
     @param data 请求数据
     */
    public function get($data = array()){
        $data = http_build_query($data);
        $this->setget($data);
        return $this;
    }
    /*
     @desc:模拟post请求
     @param data 请求数据
     */
    public function post($data = array()){
        $this->setpost($data);
        return $this;
    }
    /*
     @desc:模拟json请求
     @param data 请求数据
     */
    public function json($data = array()){
        $data = json_encode($data);
        $header = array(
                'Content-Type: application/json',
                'Content-Length:' . strlen($data)
            );
        $this->header($header);
        $this->setpost($data);
        return $this;
    }
    /*
     @desc:模拟表单上传
     @param files 文件路径
     */
    public function upload($files){
        $this->progress = true;
        $data = array();
        $name = $this->name;
        if(is_array($files)){
            foreach($files as $k=>$v){
                $data["{$name}[{$k}]"]=new \CURLFile($v);
            }
        }else{
            $data["{$name}"]=new \CURLFile($files);
        }
        ob_start();
        echo ">>";
        ob_flush();
        flush();
        $last = 0;
        $diff = 0;
        $this->options[CURLOPT_PROGRESSFUNCTION] = function($source,$dfilesize,$downsize,$upsize,$ufilesize){
            global $last;
            global $diff;
            if($ufilesize > 0){
                $percent = round($ufilesize/$upsize*100,2);
                $diff += $percent - $last;
                if($diff > 1){
                    echo "#";
                    $diff = 0;
                }
                $last = $percent;
            }
            ob_flush();
            flush();
        };
        $this->options[CURLOPT_NOPROGRESS] = false;
        $this->setpost($data);
        return $this;
    }
    /*
     @desc:下载文件
     @param dir 存储文件目录
     */
    public function download($dir = ''){
        $this->progress = true;
        $path = $this->path;
        if($dir && !is_dir($dir)){
            mkdir($dir,0755,true);
        }
        $name = strrchr($path, '/');
        $dsep = $dir?'/':'';
        ob_start();
        echo ">>";
        ob_flush();
        flush();
        $this->fp=fopen('.'.$dsep.$dir.$name, 'w');
        $last = 0;
        $diff = 0;
        $this->options[CURLOPT_PROGRESSFUNCTION] = function($source,$dfilesize,$downsize,$upsize,$ufilesize){
            global $last;
            global $diff;
            if($dfilesize > 0){
                $percent = round($downsize/$dfilesize*100,2);
                $diff += $percent - $last;
                if($diff > 1){
                    echo "#";
                    $diff = 0;
                }
                $last = $percent;
            }
            ob_flush();
            flush();
        };
        $this->options[CURLOPT_NOPROGRESS] = false;
        $this->options[CURLOPT_FILE] = $this->fp;
        $this->setget('');
        return $this;
    }
    /*
     @desc:执行方法
     @return ret 返回的数据
     */
    public function run(){
        $ch = $this->ch;
        $this->setopt();
        $ret = curl_exec($ch);
        curl_close($ch);
        if($this->progress){
            if($this->fp){
                fclose($this->fp);
            }
            echo "#<ch;
        $this->setopt();
        curl_exec($ch);
        $ret = curl_getinfo($ch);
        curl_close($ch);
        if($this->fp){
            fclose($this->fp);
        }
        return $ret;
    }
    }
  2. 测试:

    $http = new http('www.baidu.com');
    $ret = $http->ssl(true)->get()->run();
    echo $ret;
  3. 输出:

    
    
    
    
    
    
    
    
    
    
    
    
    
    百度一下,你就知道
    
    
    
    
    
    
    
    
    搜索设置|百度首页|登录注册
新闻 网页 贴吧 知道 音乐 图片 视频 地图 文库 更多»
输入法
  • 手写
  • 拼音
  • 关闭
  • 推荐 : 百度浏览器,打开网页快2秒!
    新闻hao123地图视频贴吧

    新 闻 网 页 贴 吧 知 道 音 乐 图 片 视 频 地 图

    输入法
  • 手写
  • 拼音
  • 关闭
  • 百科 文库 hao123 | 更多>>

    把百度设为主页把百度设为主页关于百度About Baidu

    ©2018 Baidu 使用百度前必读 京ICP证030173号 

    相关内容

    热门资讯

    “中国消费者不再仰望西方文化,... 江西南昌一家律师事务所的合伙人李毛崽在开了多年的奔驰和宝马后,买了一辆国产豪华电动车;老铺黄金在设计...
    36分!北京家庭新能源车指标入... 5月26日,北京市机动车调控管理办公室发布消息,公布家庭新能源小客车指标的积分排序入围名单,以及个人...
    中国将如何支持外企投资中国、深... 【大河财立方消息】5月26日,国新办举行新闻发布会,请商务部副部长鄢东、山东省副省长温暖,商务部国际...
    资金筹措像“化缘” 谁该为公益... 救援人员转运跌落悬崖的“驴友”救援人员用装备固定伤员救援人员搜救受困人员救援人员进行高空索降4月26...
    涉广东、广西两省区,中央生态环... 第三轮第六批中央生态环境保护督察5月26日集中通报广东和广西两省区典型案例。广东省惠州阳江等地海洋生...
    马英九为何缺席记者会?金溥聪回... 据中评社5月25日报道,马英九基金会指控前执行长萧旭岑、王光慈涉违反财政纪律,调查小组24日表示,无...
    坚决打好打赢“三夏”攻坚战丨粮... 5月24日,许昌市建安区陈曹乡的一片麦田里,一名农技人员正蹲在田垄间,用手指轻轻托起一穗即将成熟的麦...
    抢学 ——张庄村与十八洞村结对... 三夏时节,中原大地麦浪泛黄。田野里,成袋的大蒜整齐码放,处处是抢收抢种的身影。一个“抢”字,是兰考农...
    坚决打好打赢“三夏”攻坚战丨凝... 5月22日,温县黄庄镇林李庄村的连片麦田里,沉甸甸的麦穗在微风中起伏,散发出阵阵麦香。温县农业农村局...
    豫疆万里情丨从“铝”途到“晶”... 【开栏的话】从黄河之畔到天山脚下,跨越三千里山河,有一种情谊叫“援疆”。第十一批对口援疆工作启动以来...