Skip to content

Commit 3ad560b

Browse files
authored
add asJson option (#67)
1 parent ff561f3 commit 3ad560b

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/Curl/Curl.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,18 @@ protected function preparePayload($data)
250250
$this->setOpt(CURLOPT_POSTFIELDS, $data);
251251
}
252252

253+
/**
254+
* Set the json payload informations to the postfield curl option.
255+
*
256+
* @param array $data The data to be sent.
257+
* @return void
258+
*/
259+
protected function prepareJsonPayload(array $data)
260+
{
261+
$this->setOpt(CURLOPT_POST, true);
262+
$this->setOpt(CURLOPT_POSTFIELDS, json_encode($data));
263+
}
264+
253265
/**
254266
* Set auth options for the current request.
255267
*
@@ -307,12 +319,17 @@ public function get($url, $data = array())
307319
*
308320
* @param string $url The url to make the post request
309321
* @param array $data Post data to pass to the url
322+
* @param boolean $asJson Whether the data should be passed as json or not. {@insce 2.2.1}
310323
* @return self
311324
*/
312-
public function post($url, $data = array())
325+
public function post($url, $data = array(), $asJson = false)
313326
{
314327
$this->setOpt(CURLOPT_URL, $url);
315-
$this->preparePayload($data);
328+
if ($asJson) {
329+
$this->prepareJsonPayload($data);
330+
} else {
331+
$this->preparePayload($data);
332+
}
316333
$this->exec();
317334
return $this;
318335
}

0 commit comments

Comments
 (0)