<?php
namespace app\service;

class Http
{
    public static function curlGet($url,$apiFields = null, $header = [])
    {
        $ch = curl_init();
        if(!empty($apiFields)){
            $i = 1;
            foreach ($apiFields as $key => $value){
                if($i == 1) {
                    if(mb_stripos($url, '?') !== FALSE) {
                        $url .= "&" ."$key=" . urlencode($value);
                    } else {
                        $url .= "?" ."$key=" . urlencode($value);
                    }
                } else {
                    $url .= "&" ."$key=" . urlencode($value);
                }
                $i ++;
            }
        }

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FAILONERROR, false);
        if(empty($header)){
            curl_setopt($ch, CURLOPT_HEADER, false);
        }else{
            curl_setopt($ch,CURLOPT_HTTPHEADER, $header);
        }

        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

        //https ignore ssl check ?
        if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ){
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        }

        $reponse = curl_exec($ch);

        if (curl_errno($ch)){
            throw new \Exception(curl_error($ch),0);
        }else{
            $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            if (200 !== $httpStatusCode){
                throw new \Exception($reponse,$httpStatusCode);
            }
        }
        curl_close($ch);
        return $reponse;
    }

    public static function curlPost($url, $postFields = null, $header = [], $hasFile = false)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FAILONERROR, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        //https 请求
        if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        }
        curl_setopt($ch, CURLOPT_POST, true);

        if (is_array($postFields) && 0 < count($postFields))
        {
            if($hasFile) {
                // CURLOPT_POSTFIELDS 值为数组则以multipart/form-data形式提交
                curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
            } else {
                $jsonHeader = array("Content-Type: application/json; charset=utf-8", "Content-Length:".strlen(json_encode($postFields)));
                $header = array_merge($header, $jsonHeader);
                curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postFields));
            }

            if(!empty($header)) {
                curl_setopt($ch,CURLOPT_HTTPHEADER, $header);
            }
        }

        $reponse = curl_exec($ch);
        if (curl_errno($ch))
        {
            throw new \Exception(curl_error($ch),0);
        }
        else
        {
            $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            if (200 !== $httpStatusCode)
            {
                throw new \Exception($reponse,$httpStatusCode);
            }
        }
        curl_close($ch);
        return $reponse;
    }

    public static function curlDelete($url, $postFields = null)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FAILONERROR, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        //https 请求
        if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        }

        if (is_array($postFields) && 0 < count($postFields))
        {
            $header = array("Content-Type: application/json; charset=utf-8", "Content-Length:".strlen(json_encode($postFields)));
            curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
            curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postFields));
        }
        $reponse = curl_exec($ch);
        if (curl_errno($ch))
        {
            throw new \Exception(curl_error($ch),0);
        }
        else
        {
            $httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            if (200 !== $httpStatusCode)
            {
                throw new \Exception($reponse,$httpStatusCode);
            }
        }
        curl_close($ch);
        return $reponse;
    }

    //POST请求的结果解析
    public static function decodePostResult($url, $param, $header = [], $hasFile = false)
    {
        $resp = [];
        try {
            $resp = json_decode(self::curlPost($url, $param, $header, $hasFile), true);
            $resp['resp_status'] = empty($resp) ? false :true;
        } catch (\Exception $e) {
            $resp = json_decode($e->getMessage(), true);
            $resp['resp_status'] = false;
        }

        return $resp;
    }

    //GET请求结果解析
    public static function decodeGetResult($url, $header=[], $param = [])
    {
        $resp = [];
        try {
            $resp = json_decode(self::curlGet($url, $param, $header), true);
            $resp['resp_status'] = empty($resp) ? false :true;
        } catch (\Exception $e) {
            $resp = json_decode($e->getMessage(), true);
            $resp['resp_status'] = false;
        }

        return $resp;
    }
}