网站建设资讯

NEWS

网站建设资讯

php中http请求类的示例分析

这篇文章将为大家详细讲解有关php中http请求类的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

站在用户的角度思考问题,与客户深入沟通,找到石嘴山网站设计与石嘴山网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:网站设计、成都网站制作、企业官网、英文网站、手机端网站、网站推广、申请域名雅安服务器托管、企业邮箱。业务覆盖石嘴山地区。

  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号 

    关于“php中http请求类的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。


    文章名称:php中http请求类的示例分析
    当前路径:http://cdweb.net/article/gppppe.html