21 private $bodyData = null;
22 private $responseHeaders = array();
23 private $resultXML = null;
24 private $result = null;
26 private $deserializationType = null;
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);
52 private function getHeaderArrayFromCurlResponse($response) {
58 if (strpos($response,
"\r\n\r\n") != strrpos($response,
"\r\n\r\n")){
59 $start = strpos($response,
"\r\n\r\n")+4;
61 $header_text = substr($response, $start, strrpos($response,
"\r\n\r\n"));
63 foreach (explode(
"\r\n", $header_text) as $i => $line) {
65 $headers[
'http_code'] = $line;
67 if (strpos($line,
':') != 0) {
68 list ($key, $value) = explode(
': ', $line);
69 $headers[$key] = $value;
77 private function getBodyFromCurlResponse($response) {
78 return substr($response, strrpos($response,
"\r\n\r\n")+4, strlen($response));
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();
91 private function checkForCURLError() {
92 if (curl_errno($this->curlSession)) {
93 $curlErrorMessage = curl_error($this->curlSession);
94 $curlErrorCode = curl_errno($this->curlSession);
99 private function checkForServerError() {
100 $statusCode = $this->statusCode;
101 if ($statusCode >= 500 && $statusCode <= 599) {
107 private function setResultFields() {
108 if ($this->bodyData) {
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);
115 if (!isset($this->result) && !is_array($this->result)) {
116 $this->result = $this->bodyData;
119 }
else if ($this->contentType ==
"application/json" || $this->contentType ==
'application/vnd.maileon.api+json') {
122 $this->result = $this->bodyData;
134 return $this->result;
142 return $this->statusCode;
150 return $this->statusCode >= 200 and $this->statusCode <= 299;
158 return $this->statusCode >= 400 and $this->statusCode <= 499;
174 return $this->bodyData;
182 return $this->resultXML;
190 return $this->responseHeaders;
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;
208 $result .=
"No body data.\n";
210 if ($this->resultXML) {
211 $result .=
"Body contains XML.\n";
213 $resultType = gettype($this->result);
214 if ($resultType ==
"object") {
215 $result .=
"Result type: " . get_class($this->result) .
"\n";
217 $result .=
"result type: " . $resultType .
"\n";
222 private function startsWith($haystack, $needle) {
224 return $needle ===
"" || strrpos($haystack, $needle, -strlen($haystack)) !== FALSE;
static json_decode($jsonString, $deserializationType=null)
static getStringFromHTTPStatusCode($httpStatusCode)
__construct($response, $curlSession, $throwException=true, $deserializationType=null)