zoukankan      html  css  js  c++  java
  • PHP之curl函数相关试题

    一.问答题

    1.curl_setopt中超时设置,URL设置,post数据接收设置,解压缩设置,HEADER信息设置的参数名分别是什么?

    2.curl批量设置参数的函数是什么?

    二.编程题

    1.封装一个curl类,提供get,post方法,通过传递url,data数据获取某个网址的内容,方法返回信息格式为array('response'=>'网页内容','status'=>'http请求状态码','error'=>'错误信息')

       要求能够改header信息,并且有超时机制,zip解压缩功能


    一.问答题

    1.CURLOPT_TIMEOUT,CURLOPT_URL,CURLOPT_POSTFILEDS,CURLOPT_ENCODING,CURLOPT_HTTPHEADER

    2.curl_setopt_array($ch,$options);

    二.编程题

    1.

    <?php
    class curl{
    
        private $ch = '';
        private $timeout = 5;
        public  $options = array();
        public  $headers = array();
        public  $url = '';
    
        function __construct()
        {
            $this -> ch = curl_init();
            $this -> headers[] = "Accept: */*";
            $this -> headers[] = "Accept-Encoding: gzip,deflate,sdch";
            $this -> headers[] = "Connection: keep-alive";
        }
    
        public function get($url,$timeout = NULL)
        {
            $this -> url = $url;
            $this -> options[CURLOPT_TIMEOUT] = $timeout?$timeout : $this -> timeout;
            $this -> setopt();
            return $this -> result();
        }
    
        public function post($url,$data,$timeout = NULL)
        {
            $this -> url = $url;
            $this -> options[CURLOPT_TIMEOUT] = $timeout?$timeout : $this -> timeout;
            $this -> options[CURLOPT_POST] = true;
            $this -> options[CURLOPT_POSTFIELDS] = $data;
            $this -> setopt();
            return $this -> result();
        }
        
        private function setopt()
        {
            curl_setopt($this -> ch, CURLOPT_URL, $this -> url);
            curl_setopt($this -> ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($this -> ch, CURLOPT_ENCODING , 'gzip');
            curl_setopt($this -> ch, CURLOPT_HTTPHEADER, $this -> headers);
            curl_setopt_array($this -> ch, $this -> options);
        }
    
        private function execs() {return curl_exec($this -> ch);}
        private function status(){return curl_getinfo($this -> ch, CURLINFO_HTTP_CODE);}
        private function error() {return (curl_errno($this -> ch))?curl_error($this -> ch) : '';}
        private function result()
        {
            $result['response'] = $this -> execs();
            $result['status'] = $this -> status();
            $result['error'] = $this -> error();
            return $result;
        }
    }
  • 相关阅读:
    Python之路,Day2
    Dnsmasq安装与配置-搭建本地DNS服务器 更干净更快无广告DNS解析
    bind+dlz+mysql实现区域记录动态更新
    SUSE下FTP服务器搭建
    最简单粗暴的http文件列表
    shell脚本监控MySQL服务是否正常
    解决问题:Jupyter Notebook启动不会自动打开浏览器,每次都要自己打开浏览器输入网址
    让Jupyter Notebook个性化
    数据预处理小结
    多模型融合推荐算法
  • 原文地址:https://www.cnblogs.com/xiaozong/p/5766160.html
Copyright © 2011-2022 走看看