Skip to content

Commit 7865e81

Browse files
authored
Merge pull request #21 from VC24/master
Apply postfields to PUT and PATCH request
2 parents 36594a2 + 2eb9abc commit 7865e81

File tree

1 file changed

+42
-24
lines changed

1 file changed

+42
-24
lines changed

src/Curl/Curl.php

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,17 @@ public function __construct()
4747
$this->init();
4848
}
4949

50+
protected function preparePayload($data)
51+
{
52+
$this->setOpt(CURLOPT_POST, true);
53+
54+
if (is_array($data) || is_object($data)) {
55+
$data = http_build_query($data);
56+
}
57+
58+
$this->setOpt(CURLOPT_POSTFIELDS, $data);
59+
}
60+
5061
public function get($url, $data = array())
5162
{
5263
if (count($data) > 0) {
@@ -55,53 +66,52 @@ public function get($url, $data = array())
5566
$this->setOpt(CURLOPT_URL, $url);
5667
}
5768
$this->setOpt(CURLOPT_HTTPGET, true);
58-
$this->_exec();
69+
$this->exec();
5970
}
6071

6172
public function post($url, $data = array())
6273
{
6374
$this->setOpt(CURLOPT_URL, $url);
64-
$this->setOpt(CURLOPT_POST, true);
65-
if (is_array($data) || is_object($data))
66-
{
67-
$data = http_build_query($data);
68-
}
69-
$this->setOpt(CURLOPT_POSTFIELDS, $data);
70-
$this->_exec();
75+
$this->preparePayload($data);
76+
$this->exec();
7177
}
7278

73-
public function put($url, $data = array(), $json = 0)
79+
public function put($url, $data = array(), $payload = false)
7480
{
75-
if ($json == 0) {
81+
if ($payload === false) {
7682
$url .= '?' . http_build_query($data);
7783
} else {
78-
$this->setOpt(CURLOPT_POST, true);
79-
80-
if (is_array($data) || is_object($data)) {
81-
$data = http_build_query($data);
82-
}
83-
84-
$this->setOpt(CURLOPT_POSTFIELDS, $data);
84+
$this->preparePayload($data);
8585
}
8686

8787
$this->setOpt(CURLOPT_URL, $url);
8888
$this->setOpt(CURLOPT_CUSTOMREQUEST, 'PUT');
89-
$this->_exec();
89+
$this->exec();
9090
}
9191

92-
public function patch($url, $data = array())
92+
public function patch($url, $data = array(), $payload = false)
9393
{
94+
if ($payload === false) {
95+
$url .= '?' . http_build_query($data);
96+
} else {
97+
$this->preparePayload($data);
98+
}
99+
94100
$this->setOpt(CURLOPT_URL, $url);
95101
$this->setOpt(CURLOPT_CUSTOMREQUEST, 'PATCH');
96-
$this->setOpt(CURLOPT_POSTFIELDS, $data);
97-
$this->_exec();
102+
$this->exec();
98103
}
99104

100-
public function delete($url, $data = array())
105+
public function delete($url, $data = array(), $payload = false)
101106
{
102-
$this->setOpt(CURLOPT_URL, $url . '?' . http_build_query($data));
107+
if ($payload === false) {
108+
$url .= '?' . http_build_query($data);
109+
} else {
110+
$this->preparePayload($data);
111+
}
112+
$this->setOpt(CURLOPT_URL, $url);
103113
$this->setOpt(CURLOPT_CUSTOMREQUEST, 'DELETE');
104-
$this->_exec();
114+
$this->exec();
105115
}
106116

107117
public function setBasicAuthentication($username, $password)
@@ -174,7 +184,15 @@ public function reset()
174184
$this->init();
175185
}
176186

187+
/**
188+
* @deprecated calling exec() directly is discouraged
189+
*/
177190
public function _exec()
191+
{
192+
return $this->exec();
193+
}
194+
195+
protected function exec()
178196
{
179197
$this->response = curl_exec($this->curl);
180198
$this->curl_error_code = curl_errno($this->curl);

0 commit comments

Comments
 (0)