From 3ad560b1fc1bbdf5c7681356ab953fb961f255e5 Mon Sep 17 00:00:00 2001 From: Basil Date: Thu, 19 Mar 2020 21:07:26 +0100 Subject: [PATCH] add asJson option (#67) --- src/Curl/Curl.php | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/Curl/Curl.php b/src/Curl/Curl.php index 7e644e5..583f112 100644 --- a/src/Curl/Curl.php +++ b/src/Curl/Curl.php @@ -250,6 +250,18 @@ protected function preparePayload($data) $this->setOpt(CURLOPT_POSTFIELDS, $data); } + /** + * Set the json payload informations to the postfield curl option. + * + * @param array $data The data to be sent. + * @return void + */ + protected function prepareJsonPayload(array $data) + { + $this->setOpt(CURLOPT_POST, true); + $this->setOpt(CURLOPT_POSTFIELDS, json_encode($data)); + } + /** * Set auth options for the current request. * @@ -307,12 +319,17 @@ public function get($url, $data = array()) * * @param string $url The url to make the post request * @param array $data Post data to pass to the url + * @param boolean $asJson Whether the data should be passed as json or not. {@insce 2.2.1} * @return self */ - public function post($url, $data = array()) + public function post($url, $data = array(), $asJson = false) { $this->setOpt(CURLOPT_URL, $url); - $this->preparePayload($data); + if ($asJson) { + $this->prepareJsonPayload($data); + } else { + $this->preparePayload($data); + } $this->exec(); return $this; }