-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathresponse.php
56 lines (47 loc) · 1009 Bytes
/
response.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
<?php
class Response
{
/** @var bool */
public $IsSuccess;
/** @var int */
public $StatusCode;
/** @var object */
public $Content;
/** @var string */
public $ErrorMessage;
/**
* @param object $ch An unclosed cURL resource.
*/
function __construct($ch) {
$response = curl_exec($ch);
$this->StatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($response === false) {
$this->IsSuccess = false;
$this->ErrorMessage = curl_error($ch);
}
else {
$this->IsSuccess =
$this->StatusCode >= 200 &&
$this->StatusCode < 300;
if ($this->IsSuccess) {
$this->Content = $response;
}
else {
$this->ErrorMessage = $this->GetErrorMessage($response);
}
}
}
function GetErrorMessage($response) {
if (!is_string($response)) {
return "An unknown error occured.";
}
$error = json_decode($response);
if (
json_last_error() == JSON_ERROR_NONE &&
property_exists($error, "Message")
) {
return $error->Message;
}
return $response;
}
}