Http.php 3.75 KB
<?php

/**
 * Created by PhpStorm.
 * User: liuziyang
 * Date: 14-2-28
 * Time: 0:47
 */
class YHMCart_Library_Http
{
    private $method = 'get';

    private $content = '';

    private $httpCode = 500;

    private $parameter = array();

    private $curlInfo = array();

    private $debug = false;

    protected $curlOpt = array(
        'SSL_VERIFYPEER' => false,
        'USERAGENT' => "YOHOBuy/2.4.0 CHNetWork/672.1.13 Darwin/13.2.0",
        'TIMEOUT' => 60,
        'CONNECTTIMEOUT' => 60
    );

    public function __construct($url, array $opt = array())
    {
        $this->url = $url;
        $this->setCurlOpt($opt);
    }

    /**
     * 设置配置
     * @param array $opt
     * @return $this
     */
    public function setCurlOpt($opt = array())
    {
        foreach ($opt as $k => $v) {
            $this->curlOpt[strtoupper($k)] = $v;
        }
        return $this;
    }

    /**
     * POST设置参数
     * @param array $parameter
     * @return $this
     */
    public function setParameterPost(array $parameter)
    {
        $this->parameter = $parameter;
        return $this;
    }

    /**
     * @return $this
     */
    public function setDebug($debug = false)
    {
        $this->debug = $debug;
        return $this;
    }

    /**
     * 请求
     * @param $method
     * @return $this
     */
    public function request($method = 'GET')
    {
        $this->method = $method;
        $this->exec();
        return $this;
    }

    /**
     * 执行请求
     */
    private function exec()
    {
        $url = trim($this->url);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_USERAGENT, $this->curlOpt['USERAGENT']);
        switch ($this->method) {
            case 'POST':
                curl_setopt($ch, CURLOPT_POST, TRUE);
                if (!empty($this->parameter)) {
                    curl_setopt($ch, CURLOPT_POSTFIELDS, $this->parameter);
                }
                break;
            case 'DELETE':
                curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
                break;
        }
        if (!empty($this->curlOpt['HTTP_VERSION'])) {
            curl_setopt($ch, CURLOPT_HTTP_VERSION, $url);
        }
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_AUTOREFERER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->curlOpt['SSL_VERIFYPEER']);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->curlOpt['CONNECTTIMEOUT']);
        curl_setopt($ch, CURLOPT_TIMEOUT, $this->curlOpt['TIMEOUT']);
        curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
        curl_setopt($ch, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
        if (!empty($this->curlOpt['HTTPHEADER'])) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $this->curlOpt['HTTPHEADER']);
        }
        $this->content = curl_exec($ch);
        $this->curlInfo = curl_getinfo($ch);
        curl_close($ch);
        $this->httpCode = $this->curlInfo['http_code'];
    }

    /**
     * 获取header长度
     * @param $ch
     * @param $header
     * @return int
     */
    public function getHeader($ch, $header)
    {
        return strlen($header);
    }

    /**
     * 获取URL返回信息
     * @param null $opt
     * @return array
     */
    public function getInfo($opt = null)
    {
        if ($opt != null && is_string($opt)) {
            return $this->curlInfo[$opt];
        }
        return $this->curlInfo;
    }

    /**
     * 获取状态
     * @return int
     */
    public function getStatus()
    {
        return $this->httpCode;
    }

    /**
     * 获取返回结果
     * @return string
     */
    public function getBody()
    {
        return $this->content;
    }
}