Http.php

简介: Http.php
<?php
namespace app\common;
use app\worm\ls\model\LSMatchErrorModel;
use think\facade\Env;
use think\facade\Log;
/**
 * Class Http
 */
class Http
{
    private static $info = [];
    /**
     * @param       $url
     * @param array $headers
     * @param array $options
     * @return \Requests_Response
     */
    public static function get($url, array $headers = [], array $options = [])
    {
        $options = self::setOptions($options);
        return self::call(__FUNCTION__, [$url, $headers, $options]);
    }
    /**
     * @param       $url
     * @param array $headers
     * @param array $options
     * @return \Requests_Response
     */
    public static function head($url, array $headers = [], array $options = [])
    {
        $options = self::setOptions($options);
        return self::call(__FUNCTION__, [$url, $headers, $options]);
    }
    /**
     * @param       $url
     * @param array $headers
     * @param array $options
     * @return \Requests_Response
     */
    public static function delete($url, array $headers = [], array $options = [])
    {
        $options = self::setOptions($options);
        return self::call(__FUNCTION__, [$url, $headers, $options]);
    }
    /**
     * @param       $url
     * @param array $headers
     * @param array $options
     * @return \Requests_Response
     */
    public static function trace($url, array $headers = [], array $options = [])
    {
        $options = self::setOptions($options);
        return self::call(__FUNCTION__, [$url, $headers, $options]);
    }
    /**
     * @param       $url
     * @param array $headers
     * @param array $data
     * @param array $options
     * @return \Requests_Response
     */
    public static function post($url, array $headers = [], $data = [], array $options = [])
    {
        $options = self::setOptions($options);
        return self::call(__FUNCTION__, [$url, $headers, $data, $options]);
    }
    /**
     * @param       $url
     * @param array $headers
     * @param array $data
     * @param array $options
     * @return \Requests_Response
     */
    public static function put($url, array $headers = [], array $data = [], array $options = [])
    {
        $options = self::setOptions($options);
        return self::call(__FUNCTION__, [$url, $headers, $data, $options]);
    }
    /**
     * @param       $url
     * @param array $headers
     * @param array $data
     * @param array $options
     * @return \Requests_Response
     */
    public static function options($url, array $headers = [], array $data = [], array $options = [])
    {
        $options = self::setOptions($options);
        return self::call(__FUNCTION__, [$url, $headers, $data, $options]);
    }
    /**
     * @param       $url
     * @param       $headers
     * @param array $data
     * @param array $options
     * @return \Requests_Response
     */
    public static function patch($url, array $headers, array $data = [], array $options = [])
    {
        $options = self::setOptions($options);
        return self::call(__FUNCTION__, [$url, $headers, $data, $options]);
    }
    /**
     * @param string $url
     * @param array $headers
     * @param array $data
     * @param string $type
     * @param array $options
     * @return \Requests_Response
     */
    public static function request($url, array $headers = [], array $data = [], $type = \Requests::GET, array $options = [])
    {
        $options = self::setOptions($options);
        return self::call(__FUNCTION__, [$url, $headers, $data, $type, $options]);
    }
    private static function getHooks()
    {
        $hooks = new \Requests_Hooks();
        $hooks->register('curl.after_request', function ($headers, $info) {
            self::$info = !is_array($info) ? [$info] : $info;
        });
        return $hooks;
    }
    private static function setOptions($options)
    {
        if (!isset($options['hooks'])) {
            $options['hooks'] = self::getHooks();
        }
        $options['idn'] = false;
        return $options;
    }
    private static function call($name, $args)
    {
        try {
            $type = $name;
            if ('request' === $name) {
                $type = isset($args[3]) ? $args[3] : $name;
            }
            $type = strtolower($type);
            $startTime = microtime(true);
            //设置url
            if ($type !== 'request_multiple') {
                $args[0] = new RequestsIRI($args[0]);
            }
            $args[4]['verify'] = false;
            $response = call_user_func_array(['Requests', $name], $args);
            if ($response instanceof \Requests_Response && is_object($response->url)) {
                $response->url = $response->url->uri;
            }
            return $response;
        } catch (\Exception $e) {
            Log::error($e->getMessage());
            $response = new \Requests_Response();
            $response->body = '';
            return $response;
        }
    }
    public function getProxyIp($force = false)
    {
        try {
            $url = 'http://dps.kdlapi.com/api/getdps/?orderid=927181368146275&num=1&pt=1&sep=1';
            $result = file_get_contents($url);
            if (empty($result)) {
                throw new \Exception('获取代理IP错误');
            }
            $result = explode(':', $result);
            $ip = $result[0];
            $port = $result[1];
            $currentProxyIp = [
                'ip' => $ip,
                'port' => $port,
                'time' => time()
            ];
            return $currentProxyIp;
        } catch (\Exception $e) {
            \Log::error($e->getMessage());
            return [];
        }
    }
    //get请求快代理ip
    public function doGetByKuaidailiIp($url, $headers = [])
    {
        try {
            $ch = curl_init();
            $tunnelhost = Env::get('kuaidailiip.tunnelhost');
            $tunnelport = Env::get('kuaidailiip.tunnelport');
            $proxy = $tunnelhost . ":" . $tunnelport;
            //隧道用户名密码
            $username = Env::get('kuaidailiip.username');
            $password = Env::get('kuaidailiip.password');
            //$ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            //设置代理
            curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
            curl_setopt($ch, CURLOPT_PROXY, $proxy);
            //设置代理用户名密码
            curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);
            curl_setopt($ch, CURLOPT_PROXYUSERPWD, "{$username}:{$password}");
            //自定义header
            if ($headers) {
                curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            }
            //自定义cookie
            curl_setopt($ch, CURLOPT_COOKIE, '');
            //使用gzip压缩传输数据让访问更快
            curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
            curl_setopt($ch, CURLOPT_TIMEOUT, 10);
            //返回response头部信息
//        curl_setopt($ch, CURLOPT_HEADER, true);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            $response = curl_exec($ch);
            //$info = curl_getinfo($ch);
            $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            $err = curl_error($ch);
            curl_close($ch);
            if ($err && !$response) {
                if ($httpCode == 407) {
                    throw new \Exception($url . '//doGetByKuaidailiIp代理IP出现错误(说明:1. 没有带上用户名密码验证2. 用户名密码错误,验证失败)' . $err);
                } else if ($httpCode == 503) {
                    throw new \Exception($url . '//doGetByKuaidailiIp代理IP出现错误(说明:隧道转发代理IP错误)' . $err);
                } else if ($httpCode == 403) {
                    throw new \Exception($url . '//doGetByKuaidailiIp代理IP出现错误(说明:Over Max Connection超过最大并发连接数,Access Forbidden禁止访问该网站)' . $err);
                }
                return false;
            }
            return $response;
        } catch (\Exception $e) {
            Log::error($e);
            return '';
        }
    }
    //post请求快代理ip
    public function doPostByKuaidailiIp($url, $headers = [])
    {
        try {
            $ch = curl_init();
            $tunnelhost = Env::get('kuaidailiip.tunnelhost');
            $tunnelport = Env::get('kuaidailiip.tunnelport');
            $proxy = $tunnelhost . ":" . $tunnelport;
            //隧道用户名密码
            $username = Env::get('kuaidailiip.username');
            $password = Env::get('kuaidailiip.password');
            //$ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            //发送post请求
            $requestData["post"] = "send post request";
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($requestData));
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            //设置代理
            curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
            curl_setopt($ch, CURLOPT_PROXY, $proxy);
            //设置代理用户名密码
            curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC);
            curl_setopt($ch, CURLOPT_PROXYUSERPWD, "{$username}:{$password}");
            //自定义header
            if ($headers) {
                curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            }
            //自定义cookie
            curl_setopt($ch, CURLOPT_COOKIE, '');
            //使用gzip压缩传输数据让访问更快
            curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
            curl_setopt($ch, CURLOPT_TIMEOUT, 10);
            //返回response头部信息
//        curl_setopt($ch, CURLOPT_HEADER, true);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            $response = curl_exec($ch);
            $err = curl_error($ch);
            //$info = curl_getinfo($ch);
            curl_close($ch);
            if ($err && !$response) {
                //关闭资源
//                throw new \Exception($url . '//doGetByKuaidailiIp代理IP出现错误' . $err);
                return false;
            }
            return $response;
        } catch (\Exception $e) {
            \Log::error($e);
            return '';
        }
    }
    public function doGet($url, $headers = [], $options = [])
    {
        try {
            $ch = curl_init();
            curl_setopt_array($ch, [
                CURLOPT_URL => $url,
                CURLOPT_TIMEOUT => 10,
                CURLOPT_CUSTOMREQUEST => "GET",
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_ENCODING => "",
                CURLOPT_SSL_VERIFYPEER => 0, // 对认证证书来源的检查
                CURLOPT_SSL_VERIFYHOST => 0,
            ]);
            if ($headers) {
                curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            }
            $forceGetIp = isset($options['forceGetIp']) && $options['forceGetIp'] ? true : false;
            if ($forceGetIp && (config("app_env") != 'local')) {
                $proxyInfo = $this->getProxyIp($forceGetIp);
                $ip = empty($proxyInfo['ip']) ? '' : $proxyInfo['ip'];
                $port = empty($proxyInfo['port']) ? '' : $proxyInfo['port'];
                if (empty($ip) || empty($port)) {
                    throw new \Exception('获取代理IP失败');
                }
                curl_setopt($ch, CURLOPT_PROXY, $ip);
                curl_setopt($ch, CURLOPT_PROXYPORT, $port);
                curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); //使用http代理模式
                curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_BASIC); //代理认证模式
            }
            if (isset($options['gzip']) && $options['gzip']) {
                curl_setopt($ch, CURLOPT_ENCODING, "gzip");
            }
            $response = curl_exec($ch);
            $err = curl_error($ch);
            curl_close($ch);
            if ($err) {
                throw new \Exception('出现错误' . $err);
            }
            return $response;
        } catch (\Exception $e) {
            if (!empty($options['disabled_log'])) {
                return '';
            }
            \Log::error($e);
            return '';
        }
    }
    public function doPost($url, $headers = [], $data = [])
    {
        try {
            $ch = curl_init();
            curl_setopt_array($ch, [
                CURLOPT_URL => $url,
                CURLOPT_TIMEOUT => 30,
                CURLOPT_CUSTOMREQUEST => "POST",
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_POST => 1,
                CURLOPT_ENCODING => "",
                CURLOPT_SSL_VERIFYPEER => 0, // 对认证证书来源的检查
                CURLOPT_SSL_VERIFYHOST => 0,
                CURLOPT_POSTFIELDS => $data
            ]);
            if ($headers) {
                curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            }
            $response = curl_exec($ch);
            $err = curl_error($ch);
            curl_close($ch);
            if ($err) {
                throw new \Exception('出现错误' . $err);
            }
            return $response;
        } catch (\Exception $e) {
            \Log::error($e->getMessage());
            return '';
        }
    }
}


目录
相关文章
|
2月前
|
安全 PHP
PHP为协议详解,以及应用
PHP为协议详解,以及应用
33 0
|
PHP
PHP $_POST
PHP $_POST
46 0
|
7月前
|
PHP
php curl获取cookie
php curl获取cookie
49 0
|
PHP
PHP解决跨域问题
PHP解决跨域问题
62 0
|
PHP
php中curl不支持https的解决办法
在php程序中使用curl去访问https站点时,报错:Protocol https not supported or disabled in libcurl 该错误信息表示php当时编译时使用的curl库不支持https, 验证方法为你的curl安装目录/bin/curl  -V  显示如下:Pr...
1855 0
|
PHP
【PHP】curl请求
【PHP】curl请求
273 0
|
前端开发 PHP
php跨域
php跨域
128 0
|
测试技术 PHP 数据格式
php 获取HTTP POST中不同格式的数据
HTTP协议中的POST 方法有多中格式的数据协议,在HTTP的head中用不同的Content-type标识.常用的有 application/x-www-form-urlencoded,这是最常见的,就是from表单的格式.
2767 0
|
Web App开发 Linux 应用服务中间件
NGINX Plus 现在完全支持 HTTP/2
 为什么要使用 HTTP/2?   HTTP/2 使数据传输更高效,对你的应用程序更安全。 HTTP/2 相比于 HTTP/1.x 有五个提高性能特点:   完全复用 – 在一个保持激活(keepalive)的连接上,HTTP/1.1 强制按严格的顺序来处理请求。
1705 0
|
移动开发 PHP 测试技术