-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathnamecheap_response.php
103 lines (94 loc) · 2.41 KB
/
namecheap_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
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
<?php
/**
* Namecheap API response handler
*
* @copyright Copyright (c) 2013, Phillips Data, Inc.
* @license http://opensource.org/licenses/mit-license.php MIT License
* @package namecheap
*/
class NamecheapResponse {
/**
* @var SimpleXMLElement The XML parsed response from the API
*/
private $xml;
/**
* @var string The raw response from the API
*/
private $raw;
/**
* Initializes the Namecheap Response
*
* @param string $response The raw XML response data from an API request
*/
public function __construct($response) {
$this->raw = $response;
try {
$this->xml = new SimpleXMLElement($this->raw);
}
catch (Exception $e) {
// Invalid response
}
}
/**
* Returns the CommandResponse
*
* @return stdClass A stdClass object representing the CommandResponses, null if invalid response
*/
public function response() {
if ($this->xml && $this->xml instanceof SimpleXMLElement) {
return $this->formatResponse($this->xml->CommandResponse);
}
return null;
}
/**
* Returns the status of the API Responses
*
* @return string The status (OK = success, ERROR = error, null = invalid responses)
*/
public function status() {
if ($this->xml && $this->xml instanceof SimpleXMLElement) {
return (string)$this->xml->attributes()->Status;
}
return null;
}
/**
* Returns all errors contained in the response
*
* @return stdClass A stdClass object representing the errors in the response, false if invalid response
*/
public function errors() {
if ($this->xml && $this->xml instanceof SimpleXMLElement) {
return $this->formatResponse($this->xml->Errors);
}
return false;
}
/**
* Returns all warnings contained in the response
*
* @return stdClass A stdClass object representing the warnings in the response, false if invalid response
*/
public function warnings() {
if ($this->xml && $this->xml instanceof SimpleXMLElement) {
return $this->formatResponse($this->xml->Warnings);
}
return false;
}
/**
* Returns the raw response
*
* @return string The raw response
*/
public function raw() {
return $this->raw;
}
/**
* Formats the given $data into a stdClass object by first JSON encoding, then JSON decoding it
*
* @param mixed $data The data to convert to a stdClass object
* @return stdClass $data in a stdClass object form
*/
private function formatResponse($data) {
return json_decode(json_encode($data));
}
}
?>