@@ -264,41 +264,6 @@ public function delete($url, $query_parameters = array(), $data = array())
264264 return $ this ->exec ();
265265 }
266266
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-
302267 /**
303268 * Download
304269 *
@@ -503,25 +468,6 @@ public function head($url, $data = array())
503468 return $ this ->exec ();
504469 }
505470
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-
525471 /**
526472 * Options
527473 *
@@ -720,31 +666,8 @@ public function setDigestAuthentication($username, $password = '')
720666 */
721667 public function setCookie ($ key , $ value )
722668 {
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 ();
748671 }
749672
750673 /**
@@ -756,32 +679,9 @@ public function setCookie($key, $value)
756679 public function setCookies ($ cookies )
757680 {
758681 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 );
778683 }
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 ();
785685 }
786686
787687 /**
@@ -1126,7 +1026,7 @@ public function setTimeout($seconds)
11261026 public function setUrl ($ url , $ mixed_data = '' )
11271027 {
11281028 $ this ->baseUrl = $ url ;
1129- $ this ->url = $ this ->buildURL ($ url , $ mixed_data );
1029+ $ this ->url = $ this ->buildUrl ($ url , $ mixed_data );
11301030 $ this ->setOpt (CURLOPT_URL , $ this ->url );
11311031 }
11321032
@@ -1261,6 +1161,20 @@ private function __get_totalTime()
12611161 return $ this ->getInfo (CURLINFO_TOTAL_TIME );
12621162 }
12631163
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+
12641178 /**
12651179 * Build Url
12661180 *
@@ -1270,7 +1184,7 @@ private function __get_totalTime()
12701184 *
12711185 * @return string
12721186 */
1273- private function buildURL ($ url , $ mixed_data = '' )
1187+ private function buildUrl ($ url , $ mixed_data = '' )
12741188 {
12751189 $ query_string = '' ;
12761190 if (!empty ($ mixed_data )) {
@@ -1283,6 +1197,60 @@ private function buildURL($url, $mixed_data = '')
12831197 return $ url . $ query_string ;
12841198 }
12851199
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+
12861254 /**
12871255 * Parse Headers
12881256 *
@@ -1394,4 +1362,34 @@ private function parseResponseHeaders($raw_response_headers)
13941362 }
13951363 return $ response_headers ;
13961364 }
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+ }
13971395}
0 commit comments