| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 | <?php/** * Http.php UTF-8 * 基于CURL的Http请求类 * * @date    : 2018/4/25 12:18 * * @license 这不是一个自由软件,未经授权不许任何使用和传播。 * @author  : wuyonghong <wyh@huosdk.com> * @version : HUOSDK 8.0 */namespace huolib\tool;use think\Exception;class Http {    private static function init($params = null) {        if (function_exists('curl_init')) {            $ch = curl_init();            curl_setopt($ch, CURLOPT_HEADER, false);            curl_setopt($ch, CURLOPT_TIMEOUT, 30);            if (null !== $params) {                self::setParams($ch, $params);            }            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);            return $ch;        } else {            throw E('服务器不支持CURL');        }    }    private static function setParams($ch, $params) {        if (array_key_exists('header', $params)) {            curl_setopt($ch, CURLOPT_HTTPHEADER, $params['header']);        }        curl_setopt($ch, CURLOPT_TIMEOUT, array_key_exists('timeout', $params) ? $params['timeout'] : 30);    }    /**     * @param $ch     *     * @return mixed     * @throws Exception     */    private static function exec($ch) {        $rsp = curl_exec($ch);        if ($rsp !== false) {            curl_close($ch);            return $rsp;        } else {            $errorCode = curl_errno($ch);            $errorMsg = curl_error($ch);            curl_close($ch);            throw new Exception("curl出错,$errorMsg", $errorCode);        }    }    public static function request($url, $data = null, $method = 'get', $params = null) {        $method = strtolower($method);        return self::$method($url, $data, $params);    }    public static function get($url, $data = null, $params = null) {        $ch = self::init($params);        $url = rtrim($url, '?');        if (!is_null($data)) {            $url .= '?'.http_build_query($data);        }        curl_setopt($ch, CURLOPT_URL, $url);        return self::exec($ch);    }    public static function post($url, $data = null, $params = null) {        $ch = self::init($params);        curl_setopt($ch, CURLOPT_URL, $url);        curl_setopt($ch, CURLOPT_POST, true);        if (!is_null($data)) {            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));        }        return self::exec($ch);    }    public static function postRaw($url, $raw, $params = null) {        $ch = self::init($params);        curl_setopt($ch, CURLOPT_URL, $url);        curl_setopt($ch, CURLOPT_POST, true);        curl_setopt($ch, CURLOPT_POSTFIELDS, $raw);        return self::exec($ch);    }    /**     * @param      $url     * @param      $raw     * @param null $params     *     * @return mixed     * @throws Exception     */    public static function postRawSsl($url, $raw, $params = null) {        $ch = self::init($params);        if (!array_key_exists('cert_path', $params) || !array_key_exists('key_path', $params)            || !array_key_exists(                'ca_path', $params            )) {            throw new Exception('证书文件路径不能为空');        }        curl_setopt($ch, CURLOPT_SSLCERT, $params['cert_path']);        curl_setopt($ch, CURLOPT_SSLKEY, $params['key_path']);        curl_setopt($ch, CURLOPT_CAINFO, $params['ca_path']);        curl_setopt($ch, CURLOPT_URL, $url);        curl_setopt($ch, CURLOPT_POST, true);        curl_setopt($ch, CURLOPT_POSTFIELDS, $raw);        return self::exec($ch);    }    /**     * @param      $url     * @param      $path     * @param null $filename     * @param null $params     *     * @return bool|int     * @throws Exception     */    public static function saveImage($url, $path, $filename = null, $params = null) {        $ch = self::init($params);        curl_setopt($ch, CURLOPT_URL, $url);        $img = curl_exec($ch);        if ($img !== false) {            $file_info = curl_getinfo($ch);            curl_close($ch);        } else {            $errorCode = curl_errno($ch);            $errorMsg = curl_error($ch);            curl_close($ch);            throw new Exception("获取头像出错,$errorMsg", $errorCode);        }        $content_type = explode('/', $file_info['content_type']);        if (strtolower($content_type[0]) != 'image') {            throw new Exception('下载地址文件不是图片');        }        $file_path = '/'.trim($path, '/').'/';        if (is_null($filename)) {            $filename = md5($url);        }        $file_path .= $filename.'.'.end($content_type);        return file_put_contents($file_path, $img);    }    public static function postJson($url, $data = [], $params = null) {        $ch = self::init($params);        curl_setopt($ch, CURLOPT_URL, $url);        curl_setopt($ch, CURLOPT_POST, true);        if (!is_null($data)) {            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));        }        return self::exec($ch);    }}
 |