Http::getHttpClient()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Dongdavid\Notify\utils;
4
5
use Dongdavid\Notify\Exceptions\HttpException;
6
use GuzzleHttp\Client;
7
8
class Http
9
{
10
    // protected static $guzzleOptions = [];
11
12
    public static function getHttpClient()
13
    {
14
        return new Client();
15
        // return new Client(self::$guzzleOptions);
16
    }
17
18
    // public static function setGuzzleOptions(array $options)
19
    // {
20
    //     self::$guzzleOptions = $options;
21
    // }
22
23
    /**
24
     * [httpget get请求]
25
     * look me baby.
26
     *
27
     * @Author   DongDavid
28
     * @DateTime 2017-07-05T09:03:03+0800
29
     *
30
     * @param string $url    [请求地址]
31
     * @param array  $query  [请求参数 json字符串]
32
     * @param bool   $decode [是否json_decode 默认返回array false返回json字符串]
33
     *
34
     * @return array|string [description]
35
     */
36
    public static function get($url, $query = [], $decode = true)
37
    {
38
        try {
39
            if (!empty($query)) {
40
                $param = ['query' => $query];
41
            } else {
42
                $param = [];
43
            }
44
            $response = self::getHttpClient()
45
                ->get($url, $param)
46
                ->getBody()
47
                ->getContents();
48
49
            return true === $decode ? \json_decode($response, true) : $response;
50
        } catch (HttpException $e) {
51
            throw new HttpException($e->getMessage(), $e->getCode(), $e);
52
        }
53
    }
54
55
    /**
56
     * [httpPost post请求]
57
     * look me baby.
58
     *
59
     * @Author   DongDavid
60
     * @DateTime 2017-07-05T09:03:03+0800
61
     *
62
     * @param string $url    [请求地址]
63
     * @param array  $query  [请求参数 json字符串]
64
     * @param bool   $decode [是否json_decode 默认返回array false返回json字符串]
65
     *
66
     * @return array|string [description]
67
     */
68
    public static function post($url, $query, $decode = true)
69
    {
70
        try {
71
            $response = self::getHttpClient()
72
                ->post($url, ['json' => $query])
73
                ->getBody()
74
                ->getContents();
75
76
            return true === $decode ? \json_decode($response, true) : $response;
77
        } catch (HttpException $e) {
78
            throw new HttpException($e->getMessage(), $e->getCode(), $e);
79
        }
80
    }
81
82
    public static function test($decode = true)
83
    {
84
        $url = 'https://api.dongdavid.com/index/index/test?aa=bb';
85
86
        try {
87
            $response = self::getHttpClient()
88
                ->post($url, ['json' => ['cc' => 'dd', 'gadw2' => 4332]])
89
                ->getBody()
90
                ->getContents();
91
92
            return true === $decode ? \json_decode($response, true) : $response;
93
        } catch (HttpException $e) {
94
            throw new HttpException($e->getMessage(), $e->getCode(), $e);
95
        }
96
    }
97
}
98