1 | <?php |
||||
2 | /* (c) Anton Medvedev <[email protected]> |
||||
3 | * |
||||
4 | * For the full copyright and license information, please view the LICENSE |
||||
5 | * file that was distributed with this source code. |
||||
6 | */ |
||||
7 | |||||
8 | namespace Deployer\Utility; |
||||
9 | |||||
10 | use Deployer\Exception\HttpieException; |
||||
11 | |||||
12 | class Httpie |
||||
13 | { |
||||
14 | private $method = 'GET'; |
||||
15 | private $url = ''; |
||||
16 | private $headers = []; |
||||
17 | private $body = ''; |
||||
18 | private $curlopts = []; |
||||
19 | |||||
20 | public function __construct() |
||||
21 | { |
||||
22 | if (!extension_loaded('curl')) { |
||||
23 | throw new \Exception( |
||||
24 | "Please, install curl extension.\n" . |
||||
25 | "https://goo.gl/yTAeZh" |
||||
26 | ); |
||||
27 | } |
||||
28 | } |
||||
29 | |||||
30 | public static function get(string $url): Httpie |
||||
31 | { |
||||
32 | $http = new self; |
||||
33 | $http->method = 'GET'; |
||||
34 | $http->url = $url; |
||||
35 | return $http; |
||||
36 | } |
||||
37 | |||||
38 | public static function post(string $url): Httpie |
||||
39 | { |
||||
40 | $http = new self; |
||||
41 | $http->method = 'POST'; |
||||
42 | $http->url = $url; |
||||
43 | return $http; |
||||
44 | } |
||||
45 | |||||
46 | public function query(array $params): Httpie |
||||
47 | { |
||||
48 | $http = clone $this; |
||||
49 | $http->url .= '?' . http_build_query($params); |
||||
50 | return $http; |
||||
51 | } |
||||
52 | |||||
53 | public function header(string $header): Httpie |
||||
54 | { |
||||
55 | $http = clone $this; |
||||
56 | $http->headers[] = $header; |
||||
57 | return $http; |
||||
58 | } |
||||
59 | |||||
60 | public function body(array $data): Httpie |
||||
61 | { |
||||
62 | $http = clone $this; |
||||
63 | $http->body = json_encode($data, JSON_PRETTY_PRINT); |
||||
64 | $http->headers = array_merge($http->headers, [ |
||||
65 | 'Content-Type: application/json', |
||||
66 | 'Content-Length: ' . strlen($http->body) |
||||
67 | ]); |
||||
68 | return $http; |
||||
69 | } |
||||
70 | |||||
71 | public function form(array $data): Httpie |
||||
72 | { |
||||
73 | $http = clone $this; |
||||
74 | $http->body = http_build_query($data); |
||||
75 | $http->headers = array_merge($this->headers, [ |
||||
76 | 'Content-type: application/x-www-form-urlencoded', |
||||
77 | 'Content-Length: ' . strlen($http->body) |
||||
78 | ]); |
||||
79 | return $http; |
||||
80 | } |
||||
81 | |||||
82 | public function setopt($key, $value) |
||||
83 | { |
||||
84 | $http = clone $this; |
||||
85 | $http->curlopts[$key] = $value; |
||||
86 | return $http; |
||||
87 | } |
||||
88 | |||||
89 | public function send() |
||||
90 | { |
||||
91 | $ch = curl_init($this->url); |
||||
92 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $this->method); |
||||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
93 | curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers); |
||||
94 | curl_setopt($ch, CURLOPT_POSTFIELDS, $this->body); |
||||
95 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||||
96 | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
||||
97 | curl_setopt($ch, CURLOPT_MAXREDIRS, 10); |
||||
98 | curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); |
||||
99 | curl_setopt($ch, CURLOPT_TIMEOUT, 5); |
||||
100 | foreach ($this->curlopts as $key => $value) { |
||||
101 | curl_setopt($ch, $key, $value); |
||||
102 | } |
||||
103 | $result = curl_exec($ch); |
||||
0 ignored issues
–
show
It seems like
$ch can also be of type false ; however, parameter $ch of curl_exec() does only seem to accept resource , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
104 | if ($result === false) { |
||||
105 | $error = curl_error($ch); |
||||
0 ignored issues
–
show
It seems like
$ch can also be of type false ; however, parameter $ch of curl_error() does only seem to accept resource , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
106 | $errno = curl_errno($ch); |
||||
0 ignored issues
–
show
It seems like
$ch can also be of type false ; however, parameter $ch of curl_errno() does only seem to accept resource , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
107 | curl_close($ch); |
||||
0 ignored issues
–
show
It seems like
$ch can also be of type false ; however, parameter $ch of curl_close() does only seem to accept resource , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
108 | throw new HttpieException($error, $errno); |
||||
109 | } |
||||
110 | curl_close($ch); |
||||
111 | return $result; |
||||
112 | } |
||||
113 | |||||
114 | public function getJson() |
||||
115 | { |
||||
116 | $result = $this->send(); |
||||
117 | $response = json_decode($result, true); |
||||
0 ignored issues
–
show
It seems like
$result can also be of type true ; however, parameter $json of json_decode() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
118 | if (json_last_error() !== JSON_ERROR_NONE) { |
||||
119 | throw new \Exception('JSON Error: ' . json_last_error_msg()); |
||||
120 | } |
||||
121 | return $response; |
||||
122 | } |
||||
123 | } |
||||
124 |