diff --git a/Call/CurlCall.php b/Call/CurlCall.php index 0c6f662..8cf47ee 100644 --- a/Call/CurlCall.php +++ b/Call/CurlCall.php @@ -16,8 +16,11 @@ abstract class CurlCall implements ApiCallInterface protected $requestObject; protected $responseData; protected $responseObject; + protected $responseHeaderData; + protected $responseHeaderObject; protected $status; protected $asAssociativeArray; + protected $options = array(); /** * Class constructor @@ -25,11 +28,13 @@ abstract class CurlCall implements ApiCallInterface * @param string $url API url * @param object $requestObject Request * @param bool $asAssociativeArray Return associative array + * @param array $options Additional options for the cURL engine */ - public function __construct($url,$requestObject,$asAssociativeArray=false) + public function __construct($url,$requestObject,$asAssociativeArray=false,$options = array()) { $this->url = $url; $this->requestObject = $requestObject; + $this->options = $options; $this->asAssociativeArray = $asAssociativeArray; $this->generateRequestData(); } @@ -76,6 +81,14 @@ public function getRequestObjectRepresentation() return $dumper->dump(json_decode(json_encode($this->requestObject), true), 100); } + /** + * {@inheritdoc} + */ + public function getResponseHeaderObject() + { + return $this->responseHeaderObject; + } + /** * {@inheritdoc} */ @@ -180,9 +193,10 @@ public function getStatus() public function execute($options, $engine, $freshConnect = false) { $options['returntransfer']=true; - $options = $this->parseCurlOptions($options); + $options = $this->parseCurlOptions(array_merge($options, $this->options)); $this->makeRequest($engine, $options); $this->parseResponseData(); + $this->parseResponseHeader(); $this->status = $engine->getinfo(CURLINFO_HTTP_CODE); $result = $this->getResponseObject(); @@ -217,6 +231,43 @@ protected function parseCurlOptions($config) return $options; } + /** + * Protected method to parse HTTP headers if the exist in the response object + * + * @param $raw_headers + * + * @return array + * + */ + protected function parseHeader($raw_headers) + { + $headers = array(); + $key = ''; + + foreach(explode("\n", $raw_headers) as $i => $h) { + $h = explode(':', $h, 2); + + if (isset($h[1])) { + if (!isset($headers[$h[0]])) + $headers[$h[0]] = trim($h[1]); + elseif (is_array($headers[$h[0]])) { + $headers[$h[0]] = array_merge($headers[$h[0]], array(trim($h[1]))); + } else { + $headers[$h[0]] = array_merge(array($headers[$h[0]]), array(trim($h[1]))); + } + + $key = $h[0]; + } else { + if (substr($h[0], 0, 1) == "\t") + $headers[$key] .= "\r\n\t".trim($h[0]); + elseif (!$key) + $headers['Status'] = trim($h[0]); + } + } + + return $headers; + } + /** * {@inheritdoc} */ @@ -249,6 +300,19 @@ public function parseResponseData() "); } + public function parseResponseHeader() + { + if( $this->responseHeaderData ) { + if($this->asAssociativeArray) { + $this->responseHeaderObject = $this->parseHeader($this->responseHeaderData); + } else { + $this->responseHeaderObject = $this->responseHeaderData; + } + } else { + $this->responseHeaderObject = NULL; + } + } + /** * {@inheritdoc} */ @@ -267,4 +331,18 @@ public function makeRequest(\$curl, \$options) "); } + /** + * @param $curl + */ + public function curlExec($curl) + { + $data = $curl->exec(); + if( preg_match("/^HTTP\/\d\.\d/", $data) ) { + $tmp = explode( "\r\n\r\n", $data); + $this->responseHeaderData = $tmp[0]; + $this->responseData = $tmp[1]; + } else { + $this->responseData = $data; + } + } } diff --git a/Call/HttpDeleteJson.php b/Call/HttpDeleteJson.php index 5b4d22d..998b129 100644 --- a/Call/HttpDeleteJson.php +++ b/Call/HttpDeleteJson.php @@ -34,6 +34,6 @@ public function makeRequest($curl, $options) $curl->setopt(CURLOPT_URL, $this->url.'?'.$this->requestData); $curl->setopt(CURLOPT_CUSTOMREQUEST, 'DELETE'); $curl->setoptArray($options); - $this->responseData = $curl->exec(); + $this->curlExec($curl); } } diff --git a/Call/HttpGetHtml.php b/Call/HttpGetHtml.php index 9f3b20f..d94cb82 100644 --- a/Call/HttpGetHtml.php +++ b/Call/HttpGetHtml.php @@ -22,6 +22,7 @@ public function __construct($url,$cookie,$requestObject=null) $this->url = $url; $this->cookie = $cookie; $this->requestObject = $requestObject; + $this->generateRequestData(); } /** @@ -30,7 +31,7 @@ public function __construct($url,$cookie,$requestObject=null) public function generateRequestData() { if ($this->requestObject) { - $this->requestData = http_build_query($this->requestObject); + $this->requestData = '?'.http_build_query($this->requestObject); } } @@ -53,8 +54,9 @@ public function makeRequest($curl, $options) } $curl->setopt(CURLOPT_URL, $url); $curl->setopt(CURLOPT_COOKIE, $this->cookie); + $curl->setopt(CURLOPT_HTTPGET, TRUE); $curl->setoptArray($options); - $this->responseData = $curl->exec(); + $this->curlExec($curl); } } diff --git a/Call/HttpGetJson.php b/Call/HttpGetJson.php index eb631a3..3a20045 100644 --- a/Call/HttpGetJson.php +++ b/Call/HttpGetJson.php @@ -33,7 +33,7 @@ public function makeRequest($curl, $options) { $curl->setopt(CURLOPT_URL, $this->url.'?'.$this->requestData); $curl->setoptArray($options); - $this->responseData = $curl->exec(); + $this->curlExec($curl); } } diff --git a/Call/HttpGetXML.php b/Call/HttpGetXML.php new file mode 100644 index 0000000..ef560a7 --- /dev/null +++ b/Call/HttpGetXML.php @@ -0,0 +1,70 @@ + + */ +class HttpGetXML extends CurlCall implements ApiCallInterface +{ + /** + * ApiCall class constructor + * + * @param string $url API url + * @param object $cookie Cookie + * @param object $requestObject Request + * @param boolean $asAssociativeArray + */ + public function __construct($url,$cookie,$asAssociativeArray=false,$requestObject=null) + { + $this->url = $url; + $this->cookie = $cookie; + $this->requestObject = $requestObject; + $this->asAssociativeArray = $asAssociativeArray; + $this->generateRequestData(); + } + + /** + * {@inheritdoc} + */ + public function generateRequestData() + { + if ($this->requestObject) { + $this->requestData = '?'.http_build_query($this->requestObject); + } + } + + /** + * {@inheritdoc} + */ + public function parseResponseData() + { + if($this->asAssociativeArray) { + $xml = simplexml_load_string($this->responseData); + $json = json_encode($xml); + $this->responseObject = json_decode( $json, TRUE ); + } else { + $this->responseObject = $this->responseData; + } + } + + /** + * {@inheritdoc} + */ + public function makeRequest($curl, $options) + { + $url = $this->url; + if ($this->requestData) { + $url.= $this->requestData; + } + $curl->setopt(CURLOPT_URL, $url); + $curl->setopt(CURLOPT_COOKIE, $this->cookie); + $curl->setopt(CURLOPT_HTTPGET, TRUE); + $curl->setoptArray($options); + $this->curlExec($curl); + } + +} diff --git a/Call/HttpPost.php b/Call/HttpPost.php index 732c50e..c4a2cd9 100644 --- a/Call/HttpPost.php +++ b/Call/HttpPost.php @@ -33,7 +33,7 @@ public function makeRequest($curl, $options) $curl->setopt(CURLOPT_POST, 1); $curl->setopt(CURLOPT_POSTFIELDS, $this->requestData); $curl->setoptArray($options); - $this->responseData = $curl->exec(); + $this->curlExec($curl); } } diff --git a/Call/HttpPostJson.php b/Call/HttpPostJson.php index f110e39..10ad36f 100644 --- a/Call/HttpPostJson.php +++ b/Call/HttpPostJson.php @@ -34,7 +34,7 @@ public function makeRequest($curl, $options) $curl->setopt(CURLOPT_POST, 1); $curl->setopt(CURLOPT_POSTFIELDS, $this->requestData); $curl->setoptArray($options); - $this->responseData = $curl->exec(); + $this->curlExec($curl); } } diff --git a/Call/HttpPutJson.php b/Call/HttpPutJson.php index 65b42d0..1c3ca49 100644 --- a/Call/HttpPutJson.php +++ b/Call/HttpPutJson.php @@ -36,7 +36,7 @@ public function makeRequest($curl, $options) $curl->setopt(CURLOPT_POSTFIELDS, $this->requestData); $curl->setopt(CURLOPT_CUSTOMREQUEST, "PUT"); $curl->setoptArray($options); - $this->responseData = $curl->exec(); + $this->curlExec($curl); } }