Skip to content

Commit

Permalink
add asJson option (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
nadar authored Mar 19, 2020
1 parent ff561f3 commit 3ad560b
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/Curl/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 3ad560b

Please sign in to comment.