From 146928ad90e7c6dd2883923774763868304a30a8 Mon Sep 17 00:00:00 2001 From: Martijn Braam Date: Tue, 15 Jul 2014 14:33:39 +0200 Subject: [PATCH 1/2] Added option to use non-urlencoded payloads (like json) --- src/Curl/Curl.php | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index 48b6ae5..ab244b9 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -51,7 +51,7 @@ public function __construct() { public function get($url, $data = array()) { if (count($data) > 0) - $this->setopt(CURLOPT_URL, $url . '?' . http_build_query($data)); + $this->setopt(CURLOPT_URL, $url . '?' g. http_build_query($data)); else $this->setopt(CURLOPT_URL, $url); $this->setopt(CURLOPT_HTTPGET, TRUE); @@ -61,27 +61,40 @@ public function get($url, $data = array()) { public function post($url, $data=array()) { $this->setopt(CURLOPT_URL, $url); $this->setopt(CURLOPT_POST, TRUE); - $data = http_build_query($data); + if(is_array($data)){ + $data = http_build_query($data); + } $this->setopt(CURLOPT_POSTFIELDS, $data); $this->_exec(); } 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)){ + $data = http_build_query($data); + } + $this->setopt(CURLOPT_POSTFIELDS, $data); $this->_exec(); } public function patch($url, $data=array()) { $this->setopt(CURLOPT_URL, $url); $this->setopt(CURLOPT_CUSTOMREQUEST, 'PATCH'); - $this->setopt(CURLOPT_POSTFIELDS, $data); + if(is_array($data)){ + $data = http_build_query($data); + } + $this->setopt(CURLOPT_POSTFIELDS, $data); $this->_exec(); } public function delete($url, $data=array()) { - $this->setopt(CURLOPT_URL, $url . '?' . http_build_query($data)); + $this->setopt(CURLOPT_URL, $url); $this->setopt(CURLOPT_CUSTOMREQUEST, 'DELETE'); + if(is_array($data)){ + $data = http_build_query($data); + } + $this->setopt(CURLOPT_POSTFIELDS, $data); $this->_exec(); } From 1b9f840e51dc680fbb48cb78c9d3d11325da138d Mon Sep 17 00:00:00 2001 From: Martijn Braam Date: Tue, 15 Jul 2014 14:41:19 +0200 Subject: [PATCH 2/2] Added examples for new behavior --- README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/README.md b/README.md index 39ecab4..abd474e 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,11 @@ $curl->post('http://www.example.com/login/', array( 'username' => 'myusername', 'password' => 'mypassword', )); +// Or for json APIs +$curl->post('http://www.example.com/login/', json_encode(array( + 'username' => 'myusername', + 'password' => 'mypassword', +))); ``` ```php @@ -58,6 +63,11 @@ $curl->put('http://api.example.com/user/', array( 'first_name' => 'Zach', 'last_name' => 'Borboa', )); +// Or for json APIs +$curl->put('http://api.example.com/user/', json_encode(array( + 'first_name' => 'Zach', + 'last_name' => 'Borboa', +))); ``` ```php @@ -65,6 +75,10 @@ $curl = new Curl\Curl(); $curl->patch('http://api.example.com/profile/', array( 'image' => '@path/to/file.jpg', )); +// Or for json APIs +$curl->patch('http://api.example.com/profile/', json_encode(array( + 'image' => '@path/to/file.jpg', +))); ``` ```php