@@ -264,41 +264,6 @@ public function delete($url, $query_parameters = array(), $data = array())
264
264
return $ this ->exec ();
265
265
}
266
266
267
- /**
268
- * Download Complete
269
- *
270
- * @access private
271
- * @param $fh
272
- */
273
- private function downloadComplete ($ fh )
274
- {
275
- if (!$ this ->error && $ this ->downloadCompleteFunction ) {
276
- rewind ($ fh );
277
- $ this ->call ($ this ->downloadCompleteFunction , $ fh );
278
- $ this ->downloadCompleteFunction = null ;
279
- }
280
-
281
- if (is_resource ($ fh )) {
282
- fclose ($ fh );
283
- }
284
-
285
- // Fix "PHP Notice: Use of undefined constant STDOUT" when reading the
286
- // PHP script from stdin. Using null causes "Warning: curl_setopt():
287
- // supplied argument is not a valid File-Handle resource".
288
- if (!defined ('STDOUT ' )) {
289
- define ('STDOUT ' , fopen ('php://stdout ' , 'w ' ));
290
- }
291
-
292
- // Reset CURLOPT_FILE with STDOUT to avoid: "curl_exec(): CURLOPT_FILE
293
- // resource has gone away, resetting to default".
294
- $ this ->setOpt (CURLOPT_FILE , STDOUT );
295
-
296
- // Reset CURLOPT_RETURNTRANSFER to tell cURL to return subsequent
297
- // responses as the return value of curl_exec(). Without this,
298
- // curl_exec() will revert to returning boolean values.
299
- $ this ->setOpt (CURLOPT_RETURNTRANSFER , true );
300
- }
301
-
302
267
/**
303
268
* Download
304
269
*
@@ -503,25 +468,6 @@ public function head($url, $data = array())
503
468
return $ this ->exec ();
504
469
}
505
470
506
- /**
507
- * Create Header Callback
508
- *
509
- * @access private
510
- * @param $header_callback_data
511
- *
512
- * @return callable
513
- */
514
- private function createHeaderCallback ($ header_callback_data )
515
- {
516
- return function ($ ch , $ header ) use ($ header_callback_data ) {
517
- if (preg_match ('/^Set-Cookie:\s*([^=]+)=([^;]+)/mi ' , $ header , $ cookie ) === 1 ) {
518
- $ header_callback_data ->responseCookies [$ cookie [1 ]] = trim ($ cookie [2 ], " \n\r\t\0\x0B" );
519
- }
520
- $ header_callback_data ->rawResponseHeaders .= $ header ;
521
- return strlen ($ header );
522
- };
523
- }
524
-
525
471
/**
526
472
* Options
527
473
*
@@ -720,31 +666,8 @@ public function setDigestAuthentication($username, $password = '')
720
666
*/
721
667
public function setCookie ($ key , $ value )
722
668
{
723
- $ name_chars = array ();
724
- foreach (str_split ($ key ) as $ name_char ) {
725
- if (isset ($ this ->rfc2616 [$ name_char ])) {
726
- $ name_chars [] = $ name_char ;
727
- } else {
728
- $ name_chars [] = rawurlencode ($ name_char );
729
- }
730
- }
731
-
732
- $ value_chars = array ();
733
- foreach (str_split ($ value ) as $ value_char ) {
734
- if (isset ($ this ->rfc6265 [$ value_char ])) {
735
- $ value_chars [] = $ value_char ;
736
- } else {
737
- $ value_chars [] = rawurlencode ($ value_char );
738
- }
739
- }
740
-
741
- $ this ->cookies [implode ('' , $ name_chars )] = implode ('' , $ value_chars );
742
-
743
- // Avoid using http_build_query() as unnecessary encoding is performed.
744
- // http_build_query($this->cookies, '', '; ');
745
- $ this ->setOpt (CURLOPT_COOKIE , implode ('; ' , array_map (function ($ k , $ v ) {
746
- return $ k . '= ' . $ v ;
747
- }, array_keys ($ this ->cookies ), array_values ($ this ->cookies ))));
669
+ $ this ->setEncodedCookie ($ key , $ value );
670
+ $ this ->buildCookies ();
748
671
}
749
672
750
673
/**
@@ -756,32 +679,9 @@ public function setCookie($key, $value)
756
679
public function setCookies ($ cookies )
757
680
{
758
681
foreach ($ cookies as $ key => $ value ) {
759
- $ name_chars = array ();
760
- foreach (str_split ($ key ) as $ name_char ) {
761
- if (isset ($ this ->rfc2616 [$ name_char ])) {
762
- $ name_chars [] = $ name_char ;
763
- } else {
764
- $ name_chars [] = rawurlencode ($ name_char );
765
- }
766
- }
767
-
768
- $ value_chars = array ();
769
- foreach (str_split ($ value ) as $ value_char ) {
770
- if (isset ($ this ->rfc6265 [$ value_char ])) {
771
- $ value_chars [] = $ value_char ;
772
- } else {
773
- $ value_chars [] = rawurlencode ($ value_char );
774
- }
775
- }
776
-
777
- $ this ->cookies [implode ('' , $ name_chars )] = implode ('' , $ value_chars );
682
+ $ this ->setEncodedCookie ($ key , $ value );
778
683
}
779
-
780
- // Avoid using http_build_query() as unnecessary encoding is performed.
781
- // http_build_query($this->cookies, '', '; ');
782
- $ this ->setOpt (CURLOPT_COOKIE , implode ('; ' , array_map (function ($ k , $ v ) {
783
- return $ k . '= ' . $ v ;
784
- }, array_keys ($ this ->cookies ), array_values ($ this ->cookies ))));
684
+ $ this ->buildCookies ();
785
685
}
786
686
787
687
/**
@@ -1126,7 +1026,7 @@ public function setTimeout($seconds)
1126
1026
public function setUrl ($ url , $ mixed_data = '' )
1127
1027
{
1128
1028
$ this ->baseUrl = $ url ;
1129
- $ this ->url = $ this ->buildURL ($ url , $ mixed_data );
1029
+ $ this ->url = $ this ->buildUrl ($ url , $ mixed_data );
1130
1030
$ this ->setOpt (CURLOPT_URL , $ this ->url );
1131
1031
}
1132
1032
@@ -1261,6 +1161,20 @@ private function __get_totalTime()
1261
1161
return $ this ->getInfo (CURLINFO_TOTAL_TIME );
1262
1162
}
1263
1163
1164
+ /**
1165
+ * Build Cookies
1166
+ *
1167
+ * @access private
1168
+ */
1169
+ private function buildCookies ()
1170
+ {
1171
+ // Avoid using http_build_query() as unnecessary encoding is performed.
1172
+ // http_build_query($this->cookies, '', '; ');
1173
+ $ this ->setOpt (CURLOPT_COOKIE , implode ('; ' , array_map (function ($ k , $ v ) {
1174
+ return $ k . '= ' . $ v ;
1175
+ }, array_keys ($ this ->cookies ), array_values ($ this ->cookies ))));
1176
+ }
1177
+
1264
1178
/**
1265
1179
* Build Url
1266
1180
*
@@ -1270,7 +1184,7 @@ private function __get_totalTime()
1270
1184
*
1271
1185
* @return string
1272
1186
*/
1273
- private function buildURL ($ url , $ mixed_data = '' )
1187
+ private function buildUrl ($ url , $ mixed_data = '' )
1274
1188
{
1275
1189
$ query_string = '' ;
1276
1190
if (!empty ($ mixed_data )) {
@@ -1283,6 +1197,60 @@ private function buildURL($url, $mixed_data = '')
1283
1197
return $ url . $ query_string ;
1284
1198
}
1285
1199
1200
+ /**
1201
+ * Create Header Callback
1202
+ *
1203
+ * @access private
1204
+ * @param $header_callback_data
1205
+ *
1206
+ * @return callable
1207
+ */
1208
+ private function createHeaderCallback ($ header_callback_data )
1209
+ {
1210
+ return function ($ ch , $ header ) use ($ header_callback_data ) {
1211
+ if (preg_match ('/^Set-Cookie:\s*([^=]+)=([^;]+)/mi ' , $ header , $ cookie ) === 1 ) {
1212
+ $ header_callback_data ->responseCookies [$ cookie [1 ]] = trim ($ cookie [2 ], " \n\r\t\0\x0B" );
1213
+ }
1214
+ $ header_callback_data ->rawResponseHeaders .= $ header ;
1215
+ return strlen ($ header );
1216
+ };
1217
+ }
1218
+
1219
+ /**
1220
+ * Download Complete
1221
+ *
1222
+ * @access private
1223
+ * @param $fh
1224
+ */
1225
+ private function downloadComplete ($ fh )
1226
+ {
1227
+ if (!$ this ->error && $ this ->downloadCompleteFunction ) {
1228
+ rewind ($ fh );
1229
+ $ this ->call ($ this ->downloadCompleteFunction , $ fh );
1230
+ $ this ->downloadCompleteFunction = null ;
1231
+ }
1232
+
1233
+ if (is_resource ($ fh )) {
1234
+ fclose ($ fh );
1235
+ }
1236
+
1237
+ // Fix "PHP Notice: Use of undefined constant STDOUT" when reading the
1238
+ // PHP script from stdin. Using null causes "Warning: curl_setopt():
1239
+ // supplied argument is not a valid File-Handle resource".
1240
+ if (!defined ('STDOUT ' )) {
1241
+ define ('STDOUT ' , fopen ('php://stdout ' , 'w ' ));
1242
+ }
1243
+
1244
+ // Reset CURLOPT_FILE with STDOUT to avoid: "curl_exec(): CURLOPT_FILE
1245
+ // resource has gone away, resetting to default".
1246
+ $ this ->setOpt (CURLOPT_FILE , STDOUT );
1247
+
1248
+ // Reset CURLOPT_RETURNTRANSFER to tell cURL to return subsequent
1249
+ // responses as the return value of curl_exec(). Without this,
1250
+ // curl_exec() will revert to returning boolean values.
1251
+ $ this ->setOpt (CURLOPT_RETURNTRANSFER , true );
1252
+ }
1253
+
1286
1254
/**
1287
1255
* Parse Headers
1288
1256
*
@@ -1394,4 +1362,34 @@ private function parseResponseHeaders($raw_response_headers)
1394
1362
}
1395
1363
return $ response_headers ;
1396
1364
}
1365
+
1366
+ /**
1367
+ * Set Encoded Cookie
1368
+ *
1369
+ * @access private
1370
+ * @param $key
1371
+ * @param $value
1372
+ */
1373
+ private function setEncodedCookie ($ key , $ value )
1374
+ {
1375
+ $ name_chars = array ();
1376
+ foreach (str_split ($ key ) as $ name_char ) {
1377
+ if (isset ($ this ->rfc2616 [$ name_char ])) {
1378
+ $ name_chars [] = $ name_char ;
1379
+ } else {
1380
+ $ name_chars [] = rawurlencode ($ name_char );
1381
+ }
1382
+ }
1383
+
1384
+ $ value_chars = array ();
1385
+ foreach (str_split ($ value ) as $ value_char ) {
1386
+ if (isset ($ this ->rfc6265 [$ value_char ])) {
1387
+ $ value_chars [] = $ value_char ;
1388
+ } else {
1389
+ $ value_chars [] = rawurlencode ($ value_char );
1390
+ }
1391
+ }
1392
+
1393
+ $ this ->cookies [implode ('' , $ name_chars )] = implode ('' , $ value_chars );
1394
+ }
1397
1395
}
0 commit comments