Skip to content

Commit

Permalink
Merge pull request #10 from comstar/comstar
Browse files Browse the repository at this point in the history
Improvements, thanks to Comstar
  • Loading branch information
Maurits van der Schee committed Aug 20, 2015
2 parents babd926 + a334361 commit b92bdb1
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 9 deletions.
82 changes: 80 additions & 2 deletions Call/CurlCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,25 @@ abstract class CurlCall implements ApiCallInterface
protected $requestObject;
protected $responseData;
protected $responseObject;
protected $responseHeaderData;
protected $responseHeaderObject;
protected $status;
protected $asAssociativeArray;
protected $options = array();

/**
* Class constructor
*
* @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();
}
Expand Down Expand Up @@ -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}
*/
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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}
*/
Expand Down Expand Up @@ -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}
*/
Expand All @@ -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;
}
}
}
2 changes: 1 addition & 1 deletion Call/HttpDeleteJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
6 changes: 4 additions & 2 deletions Call/HttpGetHtml.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function __construct($url,$cookie,$requestObject=null)
$this->url = $url;
$this->cookie = $cookie;
$this->requestObject = $requestObject;
$this->generateRequestData();
}

/**
Expand All @@ -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);
}
}

Expand All @@ -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);
}

}
2 changes: 1 addition & 1 deletion Call/HttpGetJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
70 changes: 70 additions & 0 deletions Call/HttpGetXML.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
namespace Lsw\ApiCallerBundle\Call;

use Lsw\ApiCallerBundle\Helper\Curl;

/**
* cURL based API call with request data send as GET parameters
*
* @author J. Cary Howell <[email protected]>
*/
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);
}

}
2 changes: 1 addition & 1 deletion Call/HttpPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
2 changes: 1 addition & 1 deletion Call/HttpPostJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
2 changes: 1 addition & 1 deletion Call/HttpPutJson.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}

0 comments on commit b92bdb1

Please sign in to comment.