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.
Passed
Push — master ( b87887...138d7a )
by t
62:54 queued 27s
created

Http::body()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 1
eloc 6
c 2
b 1
f 0
nc 1
nop 4
dl 0
loc 9
rs 10
ccs 0
cts 6
cp 0
crap 2
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 Psr\Http\Message\ResponseInterface;
16
17
/**
18
 * 用 guzzlehttp 封装的简单的方法,更复杂的请使用 guzzlehttp
19
 */
20
class Http
21
{
22
    /**
23
     * 发送一个 GET 请求
24
     *
25
     * @param string $url 请求地址
26
     * @param array $get GET 参数
27
     * @param array $options 额外参数,默认不检测 https
28
     *
29
     * @return string
30
     */
31
    public static function get($url, $get = [], $options = [])
32
    {
33
        list($url, $query) = self::__parseUrl($url);
34
        $client = new Client(Arrays::merge(['verify' => false], $options));
35
        $response = $client->request('GET', $url, [
36
            'query' => Arrays::merge($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
        list($url, $query) = self::__parseUrl($url);
55
        $client = new Client(Arrays::merge(['verify' => false], $options));
56
        $promise = $client->requestAsync('GET', $url, [
57
            'query' => Arrays::merge($query, $get),
58
        ]);
59
        $promise->then(function (ResponseInterface $res) use ($success) {
60
            $success && $success($res->getBody()->getContents(), $res);
61
        }, function (RequestException $err) use ($error) {
62
            $error && $error($err);
63
        });
64
    }
65
66
    /**
67
     * 发送一个表单 POST 请求
68
     *
69
     * @param string $url 请求地址
70
     * @param array $post POST 参数
71
     * @param array $get GET 参数
72
     * @param array $options 额外参数,默认不检测 https
73
     *
74
     * @return string
75
     */
76
    public static function post($url, $post = [], $get = [], $options = [])
77
    {
78
        list($url, $query) = self::__parseUrl($url);
79
        $client = new Client(Arrays::merge(['verify' => false], $options));
80
        $response = $client->request('POST', $url, [
81
            'query' => Arrays::merge($query, $get),
82
            'form_params' => $post,
83
        ]);
84
        return $response->getBody()->getContents();
85
    }
86
87
    /**
88
     * 发送一个异步的表单 POST 请求
89
     *
90
     * @param string $url 请求地址
91
     * @param array $post POST 参数
92
     * @param array $get GET 参数
93
     * @param array $options 额外参数,默认不检测 https
94
     * @param callback $success 成功时的回调 function($res1, $res2) $res1 是内容,$res2 是结果对象
95
     * @param callback $error 失败时的回调 function($res) $res 是结果对象
96
     *
97
     * @return void
98
     */
99
    public static function postAsync($url, $post = [], $get = [], $options = [], $success = null, $error = null)
100
    {
101
        list($url, $query) = self::__parseUrl($url);
102
        $client = new Client(Arrays::merge(['verify' => false], $options));
103
        $promise = $client->requestAsync('POST', $url, [
104
            'query' => Arrays::merge($query, $get),
105
            'form_params' => $post,
106
        ]);
107
        $promise->then(function (ResponseInterface $res) use ($success) {
108
            $success && $success($res->getBody()->getContents(), $res);
109
        }, function (RequestException $err) use ($error) {
110
            $error && $error($err);
111
        });
112
    }
113
114
    /**
115
     * 发送一个文本 POST 请求
116
     *
117
     * @param string $url 请求地址
118
     * @param string $body POST 文本
119
     * @param array $get GET 参数
120
     * @param array $options 额外参数,默认不检测 https
121
     *
122
     * @return string
123
     */
124
    public static function body($url, $body = '', $get = [], $options = [])
125
    {
126
        list($url, $query) = self::__parseUrl($url);
127
        $client = new Client(Arrays::merge(['verify' => false], $options));
128
        $response = $client->request('POST', $url, [
129
            'query' => Arrays::merge($query, $get),
130
            'body' => $body,
131
        ]);
132
        return $response->getBody()->getContents();
133
    }
134
135
    /**
136
     * 发送一个异步的文本 POST 请求
137
     *
138
     * @param string $url 请求地址
139
     * @param string $body POST 文本
140
     * @param array $get GET 参数
141
     * @param array $options 额外参数,默认不检测 https
142
     * @param callback $success 成功时的回调 function($res1, $res2) $res1 是内容,$res2 是结果对象
143
     * @param callback $error 失败时的回调 function($res) $res 是结果对象
144
     *
145
     * @return void
146
     */
147
    public static function bodyAsync($url, $body = '', $get = [], $options = [], $success = null, $error = null)
148
    {
149
        list($url, $query) = self::__parseUrl($url);
150
        $client = new Client(Arrays::merge(['verify' => false], $options));
151
        $promise = $client->requestAsync('POST', $url, [
152
            'query' => Arrays::merge($query, $get),
153
            'body' => $body,
154
        ]);
155
        $promise->then(function (ResponseInterface $res) use ($success) {
156
            $success && $success($res->getBody()->getContents(), $res);
157
        }, function (RequestException $err) use ($error) {
158
            $error && $error($err);
159
        });
160
    }
161
162
    /**
163
     * 解析 URL
164
     *
165
     * @param string $url
166
     *
167
     * @return array
168
     */
169
    private static function __parseUrl($url)
170
    {
171
        $parts = parse_url($url);
172
        $url = $parts['scheme'] . '://' . $parts['host'];
173
        if (isset($parts['port'])) {
174
            $url .= ':' . $parts['port'];
175
        }
176
        $url .= $parts['path'];
177
        if (isset($parts['query'])) {
178
            return [$url, Arrays::explode('=', explode('&', $parts['query']))];
179
        } else {
180
            return [$url, []];
181
        }
182
    }
183
}
184