Maileon PHP client  1.2.5
Easily integrate your PHP application with Maileon.
MaileonAPIResult.php
1 <?php
2 
16  private $curlSession;
17 
18  private $statusCode;
19  private $contentType;
20 
21  private $bodyData = null;
22  private $resultXML = null;
23  private $result = null;
24 
25  private $deserializationType = null;
26 
42  public function __construct($bodyData, $curlSession, $throwException = true, $deserializationType = null)
43  {
44  $this->bodyData = $bodyData;
45  $this->curlSession = $curlSession;
46  $this->deserializationType = $deserializationType;
47  $this->checkResult($throwException);
48  }
49 
50  private function checkResult($throwException) {
51  $this->statusCode = curl_getinfo($this->curlSession, CURLINFO_HTTP_CODE);
52  $this->contentType = curl_getinfo($this->curlSession, CURLINFO_CONTENT_TYPE);
53  $this->setResultFields();
54  if ($throwException === true) {
55  $this->checkForCURLError();
56  $this->checkForServerError();
57  }
58  }
59 
60  private function checkForCURLError() {
61  if (curl_errno($this->curlSession)) {
62  $curlErrorMessage = curl_error($this->curlSession);
63  $curlErrorCode = curl_errno($this->curlSession);
64  throw new com_maileon_api_MaileonAPIException("An error occurred in the connection to the REST API. Original cURL error message: ${curlErrorMessage}", $curlErrorCode);
65  }
66  }
67 
68  private function checkForServerError() {
69  $statusCode = $this->statusCode;
70  if ($statusCode >= 500 && $statusCode <= 599) {
71  throw new com_maileon_api_MaileonAPIException("A server error occurred in the REST API (HTTP status code ${statusCode}).",
72  $this->bodyData);
73  }
74  }
75 
76  private function setResultFields() {
77  if ($this->bodyData) {
78  // AddressCheck uses application/xml;charset=utf-8 content type
79  if ($this->contentType == 'application/vnd.maileon.api+xml' || $this->contentType == 'application/xml;charset=utf-8') {
80  if ($this->startsWith(trim($this->bodyData), "<")) {
81  $this->resultXML = new SimpleXMLElement($this->bodyData);
82  $this->result = com_maileon_api_xml_XMLDeserializer::deserialize($this->resultXML);
83  }
84  if (!isset($this->result) && !is_array($this->result)) {
85  $this->result = $this->bodyData;
86  }
87 
88  } else if ($this->contentType == "application/json" || $this->contentType == 'application/vnd.maileon.api+json') {
89  $this->result = com_maileon_api_json_JSONDeserializer::json_decode($this->bodyData, $this->deserializationType);
90  } else {
91  $this->result = $this->bodyData;
92  }
93  }
94  }
95 
102  public function getResult() {
103  return $this->result;
104  }
105 
110  public function getStatusCode() {
111  return $this->statusCode;
112  }
113 
118  public function isSuccess() {
119  return $this->statusCode >= 200 and $this->statusCode <= 299;
120  }
121 
126  public function isClientError() {
127  return $this->statusCode >= 400 and $this->statusCode <= 499;
128  }
129 
134  public function getContentType() {
135  return $this->getContentType();
136  }
137 
142  public function getBodyData() {
143  return $this->bodyData;
144  }
145 
150  public function getResultXML() {
151  return $this->resultXML;
152  }
153 
158  public function toString() {
159  $result = "";
160  $result .= "status code: " . $this->getStatusCode() . " "
162  $result .= "is success: " . ($this->isSuccess() ? "true" : "false") . "\n";
163  $result .= "is client error: " . ($this->isClientError() ? "true" : "false") . "\n";
164  if ($this->bodyData) {
165  $result .= "\nbody data:\n";
166  $result .= $this->bodyData;
167  $result .= "\n\n";
168  } else {
169  $result .= "No body data.\n";
170  }
171  if ($this->resultXML) {
172  $result .= "Body contains XML.\n";
173  }
174  $resultType = gettype($this->result);
175  if ($resultType == "object") {
176  $result .= "Result type: " . get_class($this->result) . "\n";
177  } else {
178  $result .= "result type: " . $resultType . "\n";
179  }
180  return $result;
181  }
182 
183  private function startsWith($haystack, $needle) {
184  // search backwards starting from haystack length characters from the end
185  return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== FALSE;
186  }
187 }
188 
189 ?>
static json_decode($jsonString, $deserializationType=null)
__construct($bodyData, $curlSession, $throwException=true, $deserializationType=null)
static getStringFromHTTPStatusCode($httpStatusCode)