Maileon PHP client  1.5.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 $responseHeaders = array();
23  private $resultXML = null;
24  private $result = null;
25 
26  private $deserializationType = null;
27 
43  public function __construct($response, $curlSession, $throwException = true, $deserializationType = null) {
44  $this->bodyData = $this->getBodyFromCurlResponse($response);
45  $this->curlSession = $curlSession;
46  $this->deserializationType = $deserializationType;
47  $this->responseHeaders = $this->getHeaderArrayFromCurlResponse($response);
48  $this->checkResult($throwException);
49  }
50 
51  // See https://stackoverflow.com/questions/10589889/returning-header-as-array-using-curl
52  private function getHeaderArrayFromCurlResponse($response) {
53  $headers = array();
54 
55  $start = 0;
56 
57  // Check if there is a proxy
58  if (strpos($response, "\r\n\r\n") != strrpos($response, "\r\n\r\n")){
59  $start = strpos($response, "\r\n\r\n")+4;
60  }
61  $header_text = substr($response, $start, strrpos($response, "\r\n\r\n"));
62 
63  foreach (explode("\r\n", $header_text) as $i => $line) {
64  if ($i === 0) {
65  $headers['http_code'] = $line;
66  } else {
67  if (strpos($line, ':') != 0) {
68  list ($key, $value) = explode(': ', $line);
69  $headers[$key] = $value;
70  }
71  }
72  }
73 
74  return $headers;
75  }
76 
77  private function getBodyFromCurlResponse($response) {
78  return substr($response, strrpos($response, "\r\n\r\n")+4, strlen($response));
79  }
80 
81  private function checkResult($throwException) {
82  $this->statusCode = curl_getinfo($this->curlSession, CURLINFO_HTTP_CODE);
83  $this->contentType = curl_getinfo($this->curlSession, CURLINFO_CONTENT_TYPE);
84  $this->setResultFields();
85  if ($throwException === true) {
86  $this->checkForCURLError();
87  $this->checkForServerError();
88  }
89  }
90 
91  private function checkForCURLError() {
92  if (curl_errno($this->curlSession)) {
93  $curlErrorMessage = curl_error($this->curlSession);
94  $curlErrorCode = curl_errno($this->curlSession);
95  throw new com_maileon_api_MaileonAPIException("An error occurred in the connection to the REST API. Original cURL error message: ${curlErrorMessage}", $curlErrorCode);
96  }
97  }
98 
99  private function checkForServerError() {
100  $statusCode = $this->statusCode;
101  if ($statusCode >= 500 && $statusCode <= 599) {
102  throw new com_maileon_api_MaileonAPIException("A server error occurred in the REST API (HTTP status code ${statusCode}).",
103  $this->bodyData);
104  }
105  }
106 
107  private function setResultFields() {
108  if ($this->bodyData) {
109  // AddressCheck uses application/xml;charset=utf-8 content type
110  if ($this->contentType == 'application/vnd.maileon.api+xml' || $this->contentType == 'application/xml;charset=utf-8') {
111  if ($this->startsWith(trim($this->bodyData), "<")) {
112  $this->resultXML = new SimpleXMLElement($this->bodyData);
113  $this->result = com_maileon_api_xml_XMLDeserializer::deserialize($this->resultXML);
114  }
115  if (!isset($this->result) && !is_array($this->result)) {
116  $this->result = $this->bodyData;
117  }
118 
119  } else if ($this->contentType == "application/json" || $this->contentType == 'application/vnd.maileon.api+json') {
120  $this->result = com_maileon_api_json_JSONDeserializer::json_decode($this->bodyData, $this->deserializationType);
121  } else {
122  $this->result = $this->bodyData;
123  }
124  }
125  }
126 
133  public function getResult() {
134  return $this->result;
135  }
136 
141  public function getStatusCode() {
142  return $this->statusCode;
143  }
144 
149  public function isSuccess() {
150  return $this->statusCode >= 200 and $this->statusCode <= 299;
151  }
152 
157  public function isClientError() {
158  return $this->statusCode >= 400 and $this->statusCode <= 499;
159  }
160 
165  public function getContentType() {
166  return $this->getContentType();
167  }
168 
173  public function getBodyData() {
174  return $this->bodyData;
175  }
176 
181  public function getResultXML() {
182  return $this->resultXML;
183  }
184 
189  public function getResponseHeaders() {
190  return $this->responseHeaders;
191  }
192 
197  public function toString() {
198  $result = "";
199  $result .= "status code: " . $this->getStatusCode() . " "
201  $result .= "is success: " . ($this->isSuccess() ? "true" : "false") . "\n";
202  $result .= "is client error: " . ($this->isClientError() ? "true" : "false") . "\n";
203  if ($this->bodyData) {
204  $result .= "\nbody data:\n";
205  $result .= $this->bodyData;
206  $result .= "\n\n";
207  } else {
208  $result .= "No body data.\n";
209  }
210  if ($this->resultXML) {
211  $result .= "Body contains XML.\n";
212  }
213  $resultType = gettype($this->result);
214  if ($resultType == "object") {
215  $result .= "Result type: " . get_class($this->result) . "\n";
216  } else {
217  $result .= "result type: " . $resultType . "\n";
218  }
219  return $result;
220  }
221 
222  private function startsWith($haystack, $needle) {
223  // search backwards starting from haystack length characters from the end
224  return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== FALSE;
225  }
226 }
static json_decode($jsonString, $deserializationType=null)
static getStringFromHTTPStatusCode($httpStatusCode)
__construct($response, $curlSession, $throwException=true, $deserializationType=null)