<?php
namespace crmeb\services;
class HttpClient {

    public $http_code;
    public $url;
    public $host = "http://202.104.113.60";
    public $timeout = 30;
    public $connectTimeout = 30;
    public $decode_json = TRUE;
    public $http_info;
    public $http_header;
    public $debug = FALSE;
    public $debugInfo = array();
    public $errorInfo = '';

    function __construct($host = null, $port = 80) {
        $this->host = $host;
    }

    public function getErrorInfo() {
        return $this->errorInfo;
    }

    public function getHttpCode() {
        if ($this->http_info) {
            return $this->http_info['http_code'];
        }
        return "unknow";
    }

    function setHost($host = null, $port = 80) {
        $this->host = $host;
        $this->port = $port;
    }

    function setDebug($debug) {
        $this->debug = $debug;
    }

    function get($url, $parameters = array(), $headers = array()) {
        $response = $this->oAuthRequest($url, 'GET', $parameters, $headers);
        if ($this->decode_json) {
            return json_decode($response, true);
        }
        return $response;
    }

    function post($url, $parameters = array(), $headers = array()) {
        $response = $this->oAuthRequest($url, 'POST', $parameters, $headers);
        if ($this->decode_json) {
            return json_decode($response, true);
        }
        return $response;
    }

    function delete($url, $parameters = array(), $headers = array()) {
        $response = $this->oAuthRequest($url, 'DELETE', $parameters);
        if ($this->decode_json) {
            return json_decode($response, true);
        }
        return $response;
    }

    function oAuthRequest($url, $method, $parameters, $headers = array()) {
        /* 		if (strrpos($url, 'http://') !== 0 && strrpos($url, 'https://') !== 0) {
          $url = "{$this->host}{$url}";
          } */

        switch ($method) {
            case 'GET':
                $url = $url . '?' . http_build_query($parameters);
                return $this->http($url, 'GET');
            default:
                if (is_array($parameters) || is_object($parameters)) {
                    $parameters = http_build_query($parameters);
                }
                return $this->http($url, $method, $parameters, $headers);
        }
    }

    function http($url, $method, $postfields = NULL, $headers = array()) {
        $this->http_info = array();

        $ssl = substr($url, 0, 8) == "https://" ? true : false;

        $ci = curl_init();
        curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        //curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent);
        curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout);
        curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout);
        curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ci, CURLOPT_ENCODING, "");
        if ($ssl) {
            curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书
            curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, false); // 检查证书中是否设置域名
        }
        curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:')); //避免data数据过长问题
        curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader'));
        curl_setopt($ci, CURLOPT_HEADER, FALSE);

        switch ($method) {
            case 'POST':
                curl_setopt($ci, CURLOPT_POST, TRUE);
                if (!empty($postfields)) {
                    curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
                    $this->postdata = $postfields;
                }
                break;
            case 'DELETE':
                curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE');
                if (!empty($postfields)) {
                    $url = "{$url}?{$postfields}";
                }
        }
        curl_setopt($ci, CURLOPT_URL, $url);
        curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE);

        $response = curl_exec($ci);
        $this->http_info = curl_getinfo($ci);
        $this->url = $url;

        if ($this->debug) {
            //$this->debugInfo[] = "=====post data======";
            //$this->debugInfo[] = $postfields;

            $this->debugInfo[] = "=====headers======";
            $this->debugInfo[] = $headers;

            $this->debugInfo[] = '=====request info=====' . "";
            $this->debugInfo[] = var_export(curl_getinfo($ci), true);

            $this->debugInfo[] = '=====response=====' . "\r\n";
            $this->debugInfo[] = $response;
        }
        $er = curl_error($ci);
        curl_close($ci);
        if ($er) {
            return $this->errorInfo = "Curl error:" . $er;
        }
        return $response;
    }

    function getHeader($ch, $header) {
        $i = strpos($header, ':');
        if (!empty($i)) {
            $key = str_replace('-', '_', strtolower(substr($header, 0, $i)));
            $value = trim(substr($header, $i + 2));
            $this->http_header[$key] = $value;
        }
        return strlen($header);
    }

    public function getDebugInfo() {
        return $this->debugInfo;
    }

}

?>