@@ -33,6 +33,12 @@ class CurlHttpClient implements HttpClient
33
33
use MessageFactoryAwareTemplate;
34
34
use StreamFactoryAwareTemplate;
35
35
36
+ /**
37
+ * cURL handle opened resource
38
+ * @var resource
39
+ */
40
+ private $ handle ;
41
+
36
42
/**
37
43
* cURL handle configuration TODO change description
38
44
*
@@ -73,6 +79,16 @@ public function __construct(
73
79
);
74
80
}
75
81
82
+ /**
83
+ * Release resources if still active
84
+ */
85
+ public function __destruct ()
86
+ {
87
+ if (is_resource ($ this ->handle )) {
88
+ curl_close ($ this ->handle );
89
+ }
90
+ }
91
+
76
92
/**
77
93
* Return available options and there default values
78
94
*
@@ -166,19 +182,27 @@ public function sendRequest(RequestInterface $request)
166
182
*/
167
183
protected function request ($ options , &$ raw , &$ info )
168
184
{
169
- $ ch = curl_init ();
185
+ if (is_resource ($ this ->handle )) {
186
+ curl_reset ($ this ->handle );
187
+ } else {
188
+ $ this ->handle = curl_init ();
189
+ }
190
+
170
191
try {
171
- curl_setopt_array ($ ch , $ options );
172
- $ raw = curl_exec ($ ch );
192
+ curl_setopt_array ($ this -> handle , $ options );
193
+ $ raw = curl_exec ($ this -> handle );
173
194
174
- if (curl_errno ($ ch ) > 0 ) {
195
+ if (curl_errno ($ this -> handle ) > 0 ) {
175
196
throw new RuntimeException (
176
- sprintf ('Curl error: (%d) %s ' , curl_errno ($ ch ), curl_error ($ ch ))
197
+ sprintf (
198
+ 'Curl error: (%d) %s ' ,
199
+ curl_errno ($ this ->handle ),
200
+ curl_error ($ this ->handle )
201
+ )
177
202
);
178
203
}
179
- $ info = curl_getinfo ($ ch );
204
+ $ info = curl_getinfo ($ this -> handle );
180
205
} finally {
181
- curl_close ($ ch );
182
206
}
183
207
}
184
208
0 commit comments