21 private $bodyData = null;
22 private $resultXML = null;
23 private $result = null;
25 private $deserializationType = null;
42 public function __construct($bodyData, $curlSession, $throwException =
true, $deserializationType = null)
44 $this->bodyData = $bodyData;
45 $this->curlSession = $curlSession;
46 $this->deserializationType = $deserializationType;
47 $this->checkResult($throwException);
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();
60 private function checkForCURLError() {
61 if (curl_errno($this->curlSession)) {
62 $curlErrorMessage = curl_error($this->curlSession);
63 $curlErrorCode = curl_errno($this->curlSession);
68 private function checkForServerError() {
69 $statusCode = $this->statusCode;
70 if ($statusCode >= 500 && $statusCode <= 599) {
76 private function setResultFields() {
77 if ($this->bodyData) {
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);
84 if (!isset($this->result) && !is_array($this->result)) {
85 $this->result = $this->bodyData;
88 }
else if ($this->contentType ==
"application/json" || $this->contentType ==
'application/vnd.maileon.api+json') {
91 $this->result = $this->bodyData;
103 return $this->result;
111 return $this->statusCode;
119 return $this->statusCode >= 200 and $this->statusCode <= 299;
127 return $this->statusCode >= 400 and $this->statusCode <= 499;
143 return $this->bodyData;
151 return $this->resultXML;
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;
169 $result .=
"No body data.\n";
171 if ($this->resultXML) {
172 $result .=
"Body contains XML.\n";
174 $resultType = gettype($this->result);
175 if ($resultType ==
"object") {
176 $result .=
"Result type: " . get_class($this->result) .
"\n";
178 $result .=
"result type: " . $resultType .
"\n";
183 private function startsWith($haystack, $needle) {
185 return $needle ===
"" || strrpos($haystack, $needle, -strlen($haystack)) !== FALSE;
static json_decode($jsonString, $deserializationType=null)
__construct($bodyData, $curlSession, $throwException=true, $deserializationType=null)
static getStringFromHTTPStatusCode($httpStatusCode)