当前位置 : 主页 > 网络编程 > PHP >

Curl

来源:互联网 收集:自由互联 发布时间:2021-06-28
Curl.php ch = curl_init(); curl_setopt($this-ch, CURLOPT_HEADER, 1); //是否输出头信息,0为不输出,非零则输出 curl_setopt($this-ch, CURLOPT_RETURNTRANSFER, 1); //0为自动输出, 1为不自动输出. curl_setopt($this-ch,
Curl.php
 ch = curl_init();
        curl_setopt($this->ch, CURLOPT_HEADER, 1);            //是否输出头信息,0为不输出,非零则输出
        curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 1);    //0为自动输出, 1为不自动输出.
        curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, 5);    // 设置超时
        curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, false);
		curl_setopt($this->ch, CURLOPT_ENCODING, "gzip");
    }
    
    /**
     * Post 请求
     * @param  string $url
     * @param  array  $data
     * @return string
     */
    public function post($url, $data)
    {
        
        curl_setopt($this->ch, CURLOPT_URL, $url);
        curl_setopt($this->ch, CURLOPT_POST, 1);
        curl_setopt($this->ch, CURLOPT_POSTFIELDS, $data);
        return $this->exec();
    }
    
    /**
     * Get 请求
     * @param  string $url
     * @return string
     */
    public function get($url)
    {
        if ($this->follow){
            $this->setCookie($this->cookie);
        }
        curl_setopt($this->ch, CURLOPT_URL, $url);
        curl_setopt($this->ch, CURLOPT_POST, 0);
        return $this->exec();
    }
    
    /**
     * 设置Cookie
     * @param  string|array $cookie
     * @return this
     */
    public function setCookie($cookie)
    {
        if (is_array($cookie)){
            $cookie = implode(';', $cookie);
        }
        curl_setopt($this->ch, CURLOPT_COOKIE, $cookie);    // 设置 Cookies
        return $this;
    }
    
    /**
     * 设置请求头
     * @param  string|array $header
     * @return this
     */
    public function setHeader($header)
    {
        if (isset($header['User-Agent'])){
            curl_setopt($this->ch, CURLOPT_USERAGENT, $header['User-Agent']);
        }
        curl_setopt($this->ch, CURLOPT_HTTPHEADER, $header);    // 设置附加协议头
        return $this;
    }
    
    /**
     * 获取响应信息
     *
     * @return array
     */
    public function getInfo()
    {
        return curl_getinfo($this->ch);
    }
    
    /**
     * 发起请求
     *
     * @return string
     */
    protected function exec()
    {
        
        $response = curl_exec($this->ch);  //执行
        
        if (empty($response)) {    //获取错误,
            $this->error = curl_error($this->ch);
            return false;
        }
        
        // Header
        $header = strpos($response, "\r\n\r\n");
        $this->header = substr($response, 0, $header);    //截取头信息
        if ($this->header == "HTTP/1.1 100 Continue"){
            $response = substr($response, $header + 4);
            $header = strpos($response, "\r\n\r\n");
            $this->header = substr($response ,0 ,$header);    //截取头信息
        }
        
        // Cookies 
        if (preg_match_all("#set-cookie: (.*?)(?=;|\r)#i", $this->header, $cookie)){
            foreach ($cookie[1] as $value){
                $tmp = explode('=', $value);
                $this->cookie[trim($tmp[0])] = $value;
            }
        }
        
        return substr($response, $header + 4);
    }
}
网友评论