-
Notifications
You must be signed in to change notification settings - Fork 4
/
Request.php
executable file
·147 lines (120 loc) · 3.18 KB
/
Request.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
namespace Libraries;
/**
* Provides a top level utility for making outgoing http requests,
* get, post, patch, put and delete
*/
class Request
{
/**
* Curl resource
*
* @var [resource]
*/
private static $curl;
/**
* Curl response
*
* @var [string]
*/
private static $response;
/**
* Error message returned by curl
*
* @var [string]
*/
private static $error;
/**
* Error number returned by curl
*
* @var [int]
*/
private static $errorNo;
/**
* HTTP status code from external resource response
*
* @var [int]
*/
private static $httpStatusCode;
/**
* Initialize curl and set options appropriately
*
* @param [string] $url
* @param [string] $method
*/
public function __construct($url, $method)
{
self::$curl = curl_init();
curl_setopt(self::$curl, CURLOPT_URL, $url);
curl_setopt(self::$curl, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt(self::$curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt(self::$curl, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt(self::$curl, CURLOPT_TIMEOUT, 20);
curl_setopt(self::$curl, CURLOPT_SSL_VERIFYPEER, false);
}
/**
* Handle curl errors that might occur when making requests to external resources
*
* @return void
*/
private static function handleErrors()
{
self::$error = curl_error(self::$curl);
self::$errorNo = curl_errno(self::$curl);
self::$httpStatusCode = curl_getinfo(self::$curl, CURLINFO_HTTP_CODE);
if (0 !== self::$errorNo) {
throw new \RuntimeException(self::$error, self::$errorNo);
}
}
/**
* Make POST requests to external resources
*
* @param [string] $url external server url
* @param [array] $data data to send to server
* @return array
* @throws \Exception
*/
public static function post($url, $data, $headers = [])
{
new self($url, 'POST');
// set post fields here before executing
curl_setopt(self::$curl, CURLOPT_POSTFIELDS, JSON::stringify($data));
if (count($headers)) {
curl_setopt(self::$curl, CURLOPT_HTTPHEADER, $headers);
}
self::$response = curl_exec(self::$curl);
self::handleErrors();
curl_close(self::$curl);
return self::$response;
}
/**
* Make GET requests to external resources
*
* @param [string] $url
* @return array
*/
public static function get($url)
{
new self($url, 'GET');
self::$response = curl_exec(self::$curl);
self::handleErrors();
curl_close(self::$curl);
return self::$response;
}
/**
* Make PUT requests to external resources
*
* @param $url
* @param $data
* @return array
*/
public static function put($url, $data)
{
new self($url, 'PUT');
curl_setopt(self::$curl, CURLOPT_POSTFIELDS, $data);
self::$response = curl_exec(self::$curl);
self::handleErrors();
curl_close(self::$curl);
return self::$response;
}
}