审查视图

extend/fast/Http.php 6.0 KB
Karson authored
1 2 3 4 5
<?php

namespace fast;

/**
F4NNIU authored
6
 * Http 请求类
Karson authored
7 8 9 10 11 12
 */
class Http
{

    /**
     * 发送一个POST请求
13 14 15 16
     * @param string $url     请求URL
     * @param array  $params  请求参数
     * @param array  $options 扩展参数
     * @return mixed|string
Karson authored
17 18 19 20 21 22 23 24 25
     */
    public static function post($url, $params = [], $options = [])
    {
        $req = self::sendRequest($url, $params, 'POST', $options);
        return $req['ret'] ? $req['msg'] : '';
    }

    /**
     * 发送一个GET请求
26 27 28 29
     * @param string $url     请求URL
     * @param array  $params  请求参数
     * @param array  $options 扩展参数
     * @return mixed|string
Karson authored
30 31 32 33 34 35 36 37 38
     */
    public static function get($url, $params = [], $options = [])
    {
        $req = self::sendRequest($url, $params, 'GET', $options);
        return $req['ret'] ? $req['msg'] : '';
    }

    /**
     * CURL发送Request请求,含POST和REQUEST
39 40 41 42
     * @param string $url     请求的链接
     * @param mixed  $params  传递的参数
     * @param string $method  请求的方法
     * @param mixed  $options CURL的参数
Karson authored
43 44 45 46 47 48 49 50 51 52
     * @return array
     */
    public static function sendRequest($url, $params = [], $method = 'POST', $options = [])
    {
        $method = strtoupper($method);
        $protocol = substr($url, 0, 5);
        $query_string = is_array($params) ? http_build_query($params) : $params;

        $ch = curl_init();
        $defaults = [];
53 54
        if ('GET' == $method) {
            $geturl = $query_string ? $url . (stripos($url, "?") !== false ? "&" : "?") . $query_string : $url;
Karson authored
55
            $defaults[CURLOPT_URL] = $geturl;
56
        } else {
Karson authored
57
            $defaults[CURLOPT_URL] = $url;
58
            if ($method == 'POST') {
Karson authored
59
                $defaults[CURLOPT_POST] = 1;
60
            } else {
Karson authored
61 62
                $defaults[CURLOPT_CUSTOMREQUEST] = $method;
            }
63
            $defaults[CURLOPT_POSTFIELDS] = $params;
Karson authored
64 65
        }
66
        $defaults[CURLOPT_HEADER] = false;
Karson authored
67
        $defaults[CURLOPT_USERAGENT] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.98 Safari/537.36";
68 69
        $defaults[CURLOPT_FOLLOWLOCATION] = true;
        $defaults[CURLOPT_RETURNTRANSFER] = true;
Karson authored
70 71 72 73 74 75
        $defaults[CURLOPT_CONNECTTIMEOUT] = 3;
        $defaults[CURLOPT_TIMEOUT] = 3;

        // disable 100-continue
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
76 77 78
        if ('https' == $protocol) {
            $defaults[CURLOPT_SSL_VERIFYPEER] = false;
            $defaults[CURLOPT_SSL_VERIFYHOST] = false;
Karson authored
79 80
        }
81
        curl_setopt_array($ch, (array)$options + $defaults);
Karson authored
82 83 84 85

        $ret = curl_exec($ch);
        $err = curl_error($ch);
86
        if (false === $ret || !empty($err)) {
Karson authored
87 88 89 90
            $errno = curl_errno($ch);
            $info = curl_getinfo($ch);
            curl_close($ch);
            return [
91
                'ret'   => false,
Karson authored
92 93 94 95 96 97 98
                'errno' => $errno,
                'msg'   => $err,
                'info'  => $info,
            ];
        }
        curl_close($ch);
        return [
99
            'ret' => true,
Karson authored
100 101 102 103 104 105
            'msg' => $ret,
        ];
    }

    /**
     * 异步发送一个请求
106 107
     * @param string $url    请求的链接
     * @param mixed  $params 请求的参数
Karson authored
108 109 110 111 112 113 114 115
     * @param string $method 请求的方法
     * @return boolean TRUE
     */
    public static function sendAsyncRequest($url, $params = [], $method = 'POST')
    {
        $method = strtoupper($method);
        $method = $method == 'POST' ? 'POST' : 'GET';
        //构造传递的参数
116
        if (is_array($params)) {
Karson authored
117
            $post_params = [];
118 119
            foreach ($params as $k => &$v) {
                if (is_array($v)) {
Karson authored
120
                    $v = implode(',', $v);
121
                }
Karson authored
122 123 124
                $post_params[] = $k . '=' . urlencode($v);
            }
            $post_string = implode('&', $post_params);
125
        } else {
Karson authored
126 127 128 129
            $post_string = $params;
        }
        $parts = parse_url($url);
        //构造查询的参数
130
        if ($method == 'GET' && $post_string) {
Karson authored
131 132 133 134 135 136
            $parts['query'] = isset($parts['query']) ? $parts['query'] . '&' . $post_string : $post_string;
            $post_string = '';
        }
        $parts['query'] = isset($parts['query']) && $parts['query'] ? '?' . $parts['query'] : '';
        //发送socket请求,获得连接句柄
        $fp = fsockopen($parts['host'], isset($parts['port']) ? $parts['port'] : 80, $errno, $errstr, 3);
137 138 139
        if (!$fp) {
            return false;
        }
Karson authored
140 141 142
        //设置超时时间
        stream_set_timeout($fp, 3);
        $out = "{$method} {$parts['path']}{$parts['query']} HTTP/1.1\r\n";
143 144 145 146 147
        $out .= "Host: {$parts['host']}\r\n";
        $out .= "Content-Type: application/x-www-form-urlencoded\r\n";
        $out .= "Content-Length: " . strlen($post_string) . "\r\n";
        $out .= "Connection: Close\r\n\r\n";
        if ($post_string !== '') {
Karson authored
148
            $out .= $post_string;
149
        }
Karson authored
150 151 152 153
        fwrite($fp, $out);
        //不用关心服务器返回结果
        //echo fread($fp, 1024);
        fclose($fp);
154
        return true;
Karson authored
155 156 157 158 159
    }

    /**
     * 发送文件到客户端
     * @param string $file
160 161
     * @param bool   $delaftersend
     * @param bool   $exitaftersend
Karson authored
162 163 164
     */
    public static function sendToBrowser($file, $delaftersend = true, $exitaftersend = true)
    {
165
        if (file_exists($file) && is_readable($file)) {
Karson authored
166 167 168 169 170 171 172 173 174 175 176
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment;filename = ' . basename($file));
            header('Content-Transfer-Encoding: binary');
            header('Expires: 0');
            header('Cache-Control: must-revalidate, post-check = 0, pre-check = 0');
            header('Pragma: public');
            header('Content-Length: ' . filesize($file));
            ob_clean();
            flush();
            readfile($file);
177
            if ($delaftersend) {
Karson authored
178 179
                unlink($file);
            }
180
            if ($exitaftersend) {
Karson authored
181 182 183 184 185
                exit;
            }
        }
    }
}