@@ -47,6 +47,17 @@ public function __construct()
47
47
$ this ->init ();
48
48
}
49
49
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
+
50
61
public function get ($ url , $ data = array ())
51
62
{
52
63
if (count ($ data ) > 0 ) {
@@ -55,53 +66,52 @@ public function get($url, $data = array())
55
66
$ this ->setOpt (CURLOPT_URL , $ url );
56
67
}
57
68
$ this ->setOpt (CURLOPT_HTTPGET , true );
58
- $ this ->_exec ();
69
+ $ this ->exec ();
59
70
}
60
71
61
72
public function post ($ url , $ data = array ())
62
73
{
63
74
$ 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 ();
71
77
}
72
78
73
- public function put ($ url , $ data = array (), $ json = 0 )
79
+ public function put ($ url , $ data = array (), $ payload = false )
74
80
{
75
- if ($ json == 0 ) {
81
+ if ($ payload === false ) {
76
82
$ url .= '? ' . http_build_query ($ data );
77
83
} 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 );
85
85
}
86
86
87
87
$ this ->setOpt (CURLOPT_URL , $ url );
88
88
$ this ->setOpt (CURLOPT_CUSTOMREQUEST , 'PUT ' );
89
- $ this ->_exec ();
89
+ $ this ->exec ();
90
90
}
91
91
92
- public function patch ($ url , $ data = array ())
92
+ public function patch ($ url , $ data = array (), $ payload = false )
93
93
{
94
+ if ($ payload === false ) {
95
+ $ url .= '? ' . http_build_query ($ data );
96
+ } else {
97
+ $ this ->preparePayload ($ data );
98
+ }
99
+
94
100
$ this ->setOpt (CURLOPT_URL , $ url );
95
101
$ this ->setOpt (CURLOPT_CUSTOMREQUEST , 'PATCH ' );
96
- $ this ->setOpt (CURLOPT_POSTFIELDS , $ data );
97
- $ this ->_exec ();
102
+ $ this ->exec ();
98
103
}
99
104
100
- public function delete ($ url , $ data = array ())
105
+ public function delete ($ url , $ data = array (), $ payload = false )
101
106
{
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 );
103
113
$ this ->setOpt (CURLOPT_CUSTOMREQUEST , 'DELETE ' );
104
- $ this ->_exec ();
114
+ $ this ->exec ();
105
115
}
106
116
107
117
public function setBasicAuthentication ($ username , $ password )
@@ -174,7 +184,15 @@ public function reset()
174
184
$ this ->init ();
175
185
}
176
186
187
+ /**
188
+ * @deprecated calling exec() directly is discouraged
189
+ */
177
190
public function _exec ()
191
+ {
192
+ return $ this ->exec ();
193
+ }
194
+
195
+ protected function exec ()
178
196
{
179
197
$ this ->response = curl_exec ($ this ->curl );
180
198
$ this ->curl_error_code = curl_errno ($ this ->curl );
0 commit comments