Permalink
Browse files

Send POST data by different communication method

  • Loading branch information...
zeleznypa committed Jul 11, 2013
1 parent e09ea9c commit 066575d66d9129cfad567d012a147ad473dd0e85
Showing with 146 additions and 0 deletions.
  1. +146 −0 Zeleznypa/Curl/Curl.php
@@ -9,15 +9,42 @@
class Curl extends \Zeleznypa\Curl\SimpleCurl
{
const
DELETE = 'DELETE',
GET = 'GET',
POST = 'POST',
PUT = 'PUT';
/** @var array $arguments */
private $arguments = array();
/** @var string $communicationMethod */
private $communicationMethod = self::GET;
/** @var mixed $data */
private $data;
/** @var string $endpoint */
private $endpoint;
/** @var callable $serializeDataFunction */
private $serializeDataFunction;
/** @var string $url */
private $url;
/**
* Constructor
* @author Pavel Železný <info@pavelzelezny.cz>
* @param string $url
* @return void
*/
public function __construct($url = NULL)
{
parent::__construct($url);
$this->setSerializeDataFunction(array($this, 'serializeData'));
}
/**
* URL argument getter
* @author Pavel Železný <info@pavelzelezny.cz>
@@ -90,6 +117,55 @@ public function setArguments(array $arguments, $overwrite = FALSE)
return $this;
}
/**
* Communication method getter
* @author Pavel Železný <info@pavelzelezny.cz>
* @return string
*/
public function getCommunicationMethod()
{
return $this->communicationMethod;
}
/**
* Communication method setter
* @author Pavel Železný <info@pavelzelezny.cz>
* @param string $communicationMethod
* @return \Zeleznypa\Curl\Curl Provides fluent interface
* @throws \UnexpectedValueException
*/
public function setCommunicationMethod($communicationMethod)
{
if (in_array($communicationMethod, $this->getAvailableCommunicationMethod()) === FALSE)
{
throw new \UnexpectedValueException('Requested communication method is not from allowed one ( ' . implode(', ', $this->getAvailableCommunicationMethod()) . ' )');
}
$this->communicationMethod = $communicationMethod;
return $this;
}
/**
* Post data getter
* @author Pavel Železný <info@pavelzelezny.cz>
* @return mixed
*/
public function getData()
{
return $this->data;
}
/**
* Post data setter
* @author Pavel Železný <info@pavelzelezny.cz>
* @param mixed $data
* @return \Zeleznypa\Curl\Curl Provides fluent interface
*/
public function setData($data)
{
$this->data = $data;
return $this;
}
/**
* Endpoint getter
* @author Pavel Železný <info@pavelzelezny.cz>
@@ -112,6 +188,33 @@ public function setEndpoint($endpoint)
return $this;
}
/**
* Serialize data function setter
* @author Pavel Železný <info@pavelzelezny.cz>
* @return callable
*/
public function getSerializeDataFunction()
{
return $this->serializeDataFunction;
}
/**
* Serialize data function setter
* @author Pavel Železný <info@pavelzelezny.cz>
* @param callable $serializeDataFunction
* @return \Zeleznypa\Curl\Curl Provides fluent interface
* @throws \UnexpectedValueException
*/
public function setSerializeDataFunction($serializeDataFunction)
{
if (is_callable($serializeDataFunction) === FALSE)
{
throw new \UnexpectedValueException('Serialize data function have to be callable');
}
$this->serializeDataFunction = $serializeDataFunction;
return $this;
}
/**
* Get cURL destination address
* @author Pavel Železný <info@pavelzelezny.cz>
@@ -134,6 +237,31 @@ public function setUrl($url)
return $this;
}
/**
* Available communication method getter
* @author Pavel Železný <info@pavelzelezny.cz>
* @return array
*/
public function getAvailableCommunicationMethod()
{
return array(
self::DELETE,
self::GET,
self::POST,
self::PUT,
);
}
/**
* Real request post fields data getter
* @author Pavel Železný <info@pavelzelezny.cz>
* @return string
*/
public function getRequestPostFields()
{
return call_user_func($this->getSerializeDataFunction(), $this->getData());
}
/**
* Real request url getter
* @author Pavel Železný <info@pavelzelezny.cz>
@@ -175,9 +303,17 @@ public function getRequestOptions()
protected function getDefaultOptions()
{
$options[CURLOPT_CONNECTTIMEOUT] = 30;
$options[CURLOPT_CUSTOMREQUEST] = $this->getCommunicationMethod();
$options[CURLOPT_POST] = $this->getCommunicationMethod() !== self::GET;
$options[CURLOPT_RETURNTRANSFER] = TRUE;
$options[CURLOPT_TIMEOUT] = 30;
$options[CURLOPT_URL] = $this->getRequestUrl();
/** Some clients are not happy, when they recieve POST data in GET request */
if ($this->getCommunicationMethod() !== self::GET)
{
$options[CURLOPT_POSTFIELDS] = $this->getRequestPostFields();
}
return $options;
}
@@ -192,4 +328,14 @@ protected function processOptions()
return $this;
}
/**
* Serialize POST data
* @author Pavel Železný <info@pavelzelezny.cz>
* @return string
*/
protected function serializeData()
{
return http_build_query($this->getData());
}
}

0 comments on commit 066575d

Please sign in to comment.