Skip to content

Commit

Permalink
Merge pull request #21 from VC24/master
Browse files Browse the repository at this point in the history
Apply postfields to PUT and PATCH request
  • Loading branch information
amouhzi authored Oct 20, 2016
2 parents 36594a2 + 2eb9abc commit 7865e81
Showing 1 changed file with 42 additions and 24 deletions.
66 changes: 42 additions & 24 deletions src/Curl/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ public function __construct()
$this->init();
}

protected function preparePayload($data)
{
$this->setOpt(CURLOPT_POST, true);

if (is_array($data) || is_object($data)) {
$data = http_build_query($data);
}

$this->setOpt(CURLOPT_POSTFIELDS, $data);
}

public function get($url, $data = array())
{
if (count($data) > 0) {
Expand All @@ -55,53 +66,52 @@ public function get($url, $data = array())
$this->setOpt(CURLOPT_URL, $url);
}
$this->setOpt(CURLOPT_HTTPGET, true);
$this->_exec();
$this->exec();
}

public function post($url, $data = array())
{
$this->setOpt(CURLOPT_URL, $url);
$this->setOpt(CURLOPT_POST, true);
if (is_array($data) || is_object($data))
{
$data = http_build_query($data);
}
$this->setOpt(CURLOPT_POSTFIELDS, $data);
$this->_exec();
$this->preparePayload($data);
$this->exec();
}

public function put($url, $data = array(), $json = 0)
public function put($url, $data = array(), $payload = false)
{
if ($json == 0) {
if ($payload === false) {
$url .= '?' . http_build_query($data);
} else {
$this->setOpt(CURLOPT_POST, true);

if (is_array($data) || is_object($data)) {
$data = http_build_query($data);
}

$this->setOpt(CURLOPT_POSTFIELDS, $data);
$this->preparePayload($data);
}

$this->setOpt(CURLOPT_URL, $url);
$this->setOpt(CURLOPT_CUSTOMREQUEST, 'PUT');
$this->_exec();
$this->exec();
}

public function patch($url, $data = array())
public function patch($url, $data = array(), $payload = false)
{
if ($payload === false) {
$url .= '?' . http_build_query($data);
} else {
$this->preparePayload($data);
}

$this->setOpt(CURLOPT_URL, $url);
$this->setOpt(CURLOPT_CUSTOMREQUEST, 'PATCH');
$this->setOpt(CURLOPT_POSTFIELDS, $data);
$this->_exec();
$this->exec();
}

public function delete($url, $data = array())
public function delete($url, $data = array(), $payload = false)
{
$this->setOpt(CURLOPT_URL, $url . '?' . http_build_query($data));
if ($payload === false) {
$url .= '?' . http_build_query($data);
} else {
$this->preparePayload($data);
}
$this->setOpt(CURLOPT_URL, $url);
$this->setOpt(CURLOPT_CUSTOMREQUEST, 'DELETE');
$this->_exec();
$this->exec();
}

public function setBasicAuthentication($username, $password)
Expand Down Expand Up @@ -174,7 +184,15 @@ public function reset()
$this->init();
}

/**
* @deprecated calling exec() directly is discouraged
*/
public function _exec()
{
return $this->exec();
}

protected function exec()
{
$this->response = curl_exec($this->curl);
$this->curl_error_code = curl_errno($this->curl);
Expand Down

0 comments on commit 7865e81

Please sign in to comment.