From 24c965971ed2fc96adfcb4d52bde8bbe67716d0a Mon Sep 17 00:00:00 2001 From: David Zurborg Date: Mon, 4 Jan 2016 16:35:46 +0100 Subject: [PATCH 1/3] Apply postfields to PUT and PATCH request Both HTTP methods allows postdata. http_build_query() will be skipped when the second argument is not an object nor an array. --- src/Curl/Curl.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index fd40b88..398d9d5 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -72,8 +72,13 @@ public function post($url, $data = array()) public function put($url, $data = array()) { - $this->setopt(CURLOPT_URL, $url . '?' . http_build_query($data)); + $this->setopt(CURLOPT_URL, $url); $this->setopt(CURLOPT_CUSTOMREQUEST, 'PUT'); + if (is_array($data) || is_object($data)) + { + $data = http_build_query($data); + } + $this->setopt(CURLOPT_POSTFIELDS, $data); $this->_exec(); } @@ -81,6 +86,10 @@ public function patch($url, $data = array()) { $this->setopt(CURLOPT_URL, $url); $this->setopt(CURLOPT_CUSTOMREQUEST, 'PATCH'); + if (is_array($data) || is_object($data)) + { + $data = http_build_query($data); + } $this->setopt(CURLOPT_POSTFIELDS, $data); $this->_exec(); } From 261ad3a1ae5de3f53535a172345de74d865e43af Mon Sep 17 00:00:00 2001 From: David Zurborg Date: Thu, 20 Oct 2016 20:04:12 +0200 Subject: [PATCH 2/3] prepare payload in separate protected method --- src/Curl/Curl.php | 51 +++++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index dceaefa..717c124 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -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) { @@ -61,27 +72,16 @@ public function get($url, $data = array()) 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->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); @@ -89,20 +89,27 @@ public function put($url, $data = array(), $json = 0) $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'); - if (is_array($data) || is_object($data)) { - $data = http_build_query($data); - } - $this->setOpt(CURLOPT_POSTFIELDS, $data); $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(); } From 2eb9abc4f37ddeb1e588a5f58811e6a0cdb3aad2 Mon Sep 17 00:00:00 2001 From: David Zurborg Date: Thu, 20 Oct 2016 20:08:49 +0200 Subject: [PATCH 3/3] rename _exec() to exec() exec() is now a protected method which should not be called from outside this class. for BC reasons, _exec() is now just a public wrapper for exec() and it is tagged as deprecated. --- src/Curl/Curl.php | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index 717c124..f36fdf8 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -66,14 +66,14 @@ 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->preparePayload($data); - $this->_exec(); + $this->exec(); } public function put($url, $data = array(), $payload = false) @@ -86,7 +86,7 @@ public function put($url, $data = array(), $payload = false) $this->setOpt(CURLOPT_URL, $url); $this->setOpt(CURLOPT_CUSTOMREQUEST, 'PUT'); - $this->_exec(); + $this->exec(); } public function patch($url, $data = array(), $payload = false) @@ -99,7 +99,7 @@ public function patch($url, $data = array(), $payload = false) $this->setOpt(CURLOPT_URL, $url); $this->setOpt(CURLOPT_CUSTOMREQUEST, 'PATCH'); - $this->_exec(); + $this->exec(); } public function delete($url, $data = array(), $payload = false) @@ -111,7 +111,7 @@ public function delete($url, $data = array(), $payload = false) } $this->setOpt(CURLOPT_URL, $url); $this->setOpt(CURLOPT_CUSTOMREQUEST, 'DELETE'); - $this->_exec(); + $this->exec(); } public function setBasicAuthentication($username, $password) @@ -184,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);