GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 8a4525...04b2e6 )
by t
05:00 queued 40s
created

Http   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 45
c 2
b 0
f 0
dl 0
loc 157
ccs 0
cts 68
cp 0
rs 10
wmc 14

7 Methods

Rating   Name   Duplication   Size   Complexity  
A download() 0 10 2
A postAsync() 0 11 3
A body() 0 8 1
A get() 0 7 1
A bodyAsync() 0 11 3
A post() 0 8 1
A getAsync() 0 10 3
1
<?php
2
/**
3
 * Class Http
4
 *
5
 * @link https://www.icy2003.com/
6
 * @author icy2003 <[email protected]>
7
 * @copyright Copyright (c) 2017, icy2003
8
 */
9
10
namespace icy2003\php\ihelpers;
11
12
use GuzzleHttp\Client;
13
use GuzzleHttp\Exception\RequestException;
14
use icy2003\php\I;
15
use icy2003\php\icomponents\file\LocalFile;
16
use Psr\Http\Message\ResponseInterface;
17
18
/**
19
 * 用 guzzlehttp 封装的简单的方法,更复杂的请使用 guzzlehttp
20
 */
21
class Http
22
{
23
    /**
24
     * 发送一个 GET 请求
25
     *
26
     * @param string $url 请求地址
27
     * @param array $get GET 参数
28
     * @param array $options 额外参数,默认不检测 https
29
     *
30
     * @return string
31
     */
32
    public static function get($url, $get = [], $options = [])
33
    {
34
        $client = new Client(Arrays::merge(['verify' => false], $options));
35
        $response = $client->request('GET', $url, [
36
            'query' => $get,
37
        ]);
38
        return $response->getBody()->getContents();
39
    }
40
41
    /**
42
     * 发送一个异步的 GET 请求
43
     *
44
     * @param string $url 请求地址
45
     * @param array $get GET 参数
46
     * @param array $options 额外参数,默认不检测 https
47
     * @param callback $success 成功时的回调 function($res1, $res2) $res1 是内容,$res2 是结果对象
48
     * @param callback $error 失败时的回调 function($res) $res 是结果对象
49
     *
50
     * @return void
51
     */
52
    public static function getAsync($url, $get = [], $options = [], $success = null, $error = null)
53
    {
54
        $client = new Client(Arrays::merge(['verify' => false], $options));
55
        $promise = $client->requestAsync('GET', $url, [
56
            'query' => $get,
57
        ]);
58
        $promise->then(function (ResponseInterface $res) use ($success) {
59
            $success && $success($res->getBody()->getContents(), $res);
60
        }, function (RequestException $err) use ($error) {
61
            $error && $error($err);
62
        });
63
    }
64
65
    /**
66
     * 发送一个表单 POST 请求
67
     *
68
     * @param string $url 请求地址
69
     * @param array $post POST 参数
70
     * @param array $get GET 参数
71
     * @param array $options 额外参数,默认不检测 https
72
     *
73
     * @return string
74
     */
75
    public static function post($url, $post = [], $get = [], $options = [])
76
    {
77
        $client = new Client(Arrays::merge(['verify' => false], $options));
78
        $response = $client->request('POST', $url, [
79
            'query' => $get,
80
            'form_params' => $post,
81
        ]);
82
        return $response->getBody()->getContents();
83
    }
84
85
    /**
86
     * 发送一个异步的表单 POST 请求
87
     *
88
     * @param string $url 请求地址
89
     * @param array $post POST 参数
90
     * @param array $get GET 参数
91
     * @param array $options 额外参数,默认不检测 https
92
     * @param callback $success 成功时的回调 function($res1, $res2) $res1 是内容,$res2 是结果对象
93
     * @param callback $error 失败时的回调 function($res) $res 是结果对象
94
     *
95
     * @return void
96
     */
97
    public static function postAsync($url, $post = [], $get = [], $options = [], $success = null, $error = null)
98
    {
99
        $client = new Client(Arrays::merge(['verify' => false], $options));
100
        $promise = $client->requestAsync('POST', $url, [
101
            'query' => $get,
102
            'form_params' => $post,
103
        ]);
104
        $promise->then(function (ResponseInterface $res) use ($success) {
105
            $success && $success($res->getBody()->getContents(), $res);
106
        }, function (RequestException $err) use ($error) {
107
            $error && $error($err);
108
        });
109
    }
110
111
    /**
112
     * 发送一个文本 POST 请求
113
     *
114
     * @param string $url 请求地址
115
     * @param string $body POST 文本
116
     * @param array $get GET 参数
117
     * @param array $options 额外参数,默认不检测 https
118
     *
119
     * @return string
120
     */
121
    public static function body($url, $body = '', $get = [], $options = [])
122
    {
123
        $client = new Client(Arrays::merge(['verify' => false], $options));
124
        $response = $client->request('POST', $url, [
125
            'query' => $get,
126
            'body' => $body,
127
        ]);
128
        return $response->getBody()->getContents();
129
    }
130
131
    /**
132
     * 发送一个异步的文本 POST 请求
133
     *
134
     * @param string $url 请求地址
135
     * @param string $body POST 文本
136
     * @param array $get GET 参数
137
     * @param array $options 额外参数,默认不检测 https
138
     * @param callback $success 成功时的回调 function($res1, $res2) $res1 是内容,$res2 是结果对象
139
     * @param callback $error 失败时的回调 function($res) $res 是结果对象
140
     *
141
     * @return void
142
     */
143
    public static function bodyAsync($url, $body = '', $get = [], $options = [], $success = null, $error = null)
144
    {
145
        $client = new Client(Arrays::merge(['verify' => false], $options));
146
        $promise = $client->requestAsync('POST', $url, [
147
            'query' => $get,
148
            'body' => $body,
149
        ]);
150
        $promise->then(function (ResponseInterface $res) use ($success) {
151
            $success && $success($res->getBody()->getContents(), $res);
152
        }, function (RequestException $err) use ($error) {
153
            $error && $error($err);
154
        });
155
    }
156
157
    /**
158
     * 下载文件
159
     *
160
     * - $file 是字符串,则会从远程文件下载到当前目录的同名文件
161
     * - $file 是数组,必须为两元素数组,如果元素二是路径,则下载到对应路径,如果元素二是文件,则下载为指定文件
162
     * - 支持中文
163
     *
164
     * @param string|array $file
165
     *
166
     * @return boolean
167
     */
168
    public function download($file)
169
    {
170
        list($from, $to) = (new LocalFile())->fileMap($file);
171
        try{
172
            (new Http())->get($from, [], [
173
                'save_to' => I::getAlias($to),
174
            ]);
175
            return true;
176
        }catch(RequestException $e){
177
            return false;
178
        }
179
    }
180
}
181