Skip to content

Commit 9eef25a

Browse files
authored
Merge pull request php-curl-class#419 from zachborboa/master
Process multicurl requests in ascending numerical order
2 parents 80f874b + 7dc6eba commit 9eef25a

File tree

6 files changed

+153
-172
lines changed

6 files changed

+153
-172
lines changed

README.md

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,6 @@ Curl::setXmlDecoder($function)
242242
Curl::success($callback)
243243
Curl::unsetHeader($key)
244244
Curl::verbose($on = true, $output = STDERR)
245-
Curl::array_flatten_multidim($array, $prefix = false)
246-
Curl::is_array_assoc($array)
247-
Curl::is_array_multidim($array)
248245
MultiCurl::__construct($base_url = null)
249246
MultiCurl::__destruct()
250247
MultiCurl::addCurl(Curl $curl)

src/Curl/ArrayUtil.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
namespace Curl;
4+
5+
class ArrayUtil
6+
{
7+
/**
8+
* Is Array Assoc
9+
*
10+
* @access public
11+
* @param $array
12+
*
13+
* @return boolean
14+
*/
15+
public static function is_array_assoc($array)
16+
{
17+
return (bool)count(array_filter(array_keys($array), 'is_string'));
18+
}
19+
20+
/**
21+
* Is Array Multidim
22+
*
23+
* @access public
24+
* @param $array
25+
*
26+
* @return boolean
27+
*/
28+
public static function is_array_multidim($array)
29+
{
30+
if (!is_array($array)) {
31+
return false;
32+
}
33+
34+
return (bool)count(array_filter($array, 'is_array'));
35+
}
36+
37+
/**
38+
* Array Flatten Multidim
39+
*
40+
* @access public
41+
* @param $array
42+
* @param $prefix
43+
*
44+
* @return array
45+
*/
46+
public static function array_flatten_multidim($array, $prefix = false)
47+
{
48+
$return = array();
49+
if (is_array($array) || is_object($array)) {
50+
if (empty($array)) {
51+
$return[$prefix] = '';
52+
} else {
53+
foreach ($array as $key => $value) {
54+
if (is_scalar($value)) {
55+
if ($prefix) {
56+
$return[$prefix . '[' . $key . ']'] = $value;
57+
} else {
58+
$return[$key] = $value;
59+
}
60+
} else {
61+
if ($value instanceof \CURLFile) {
62+
$return[$key] = $value;
63+
} else {
64+
$return = array_merge(
65+
$return,
66+
self::array_flatten_multidim(
67+
$value,
68+
$prefix ? $prefix . '[' . $key . ']' : $key
69+
)
70+
);
71+
}
72+
}
73+
}
74+
}
75+
} elseif ($array === null) {
76+
$return[$prefix] = $array;
77+
}
78+
return $return;
79+
}
80+
}

src/Curl/Curl.php

Lines changed: 35 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -7,39 +7,6 @@ class Curl
77
const VERSION = '7.1.0';
88
const DEFAULT_TIMEOUT = 30;
99

10-
public static $RFC2616 = array(
11-
// RFC2616: "any CHAR except CTLs or separators".
12-
// CHAR = <any US-ASCII character (octets 0 - 127)>
13-
// CTL = <any US-ASCII control character
14-
// (octets 0 - 31) and DEL (127)>
15-
// separators = "(" | ")" | "<" | ">" | "@"
16-
// | "," | ";" | ":" | "\" | <">
17-
// | "/" | "[" | "]" | "?" | "="
18-
// | "{" | "}" | SP | HT
19-
// SP = <US-ASCII SP, space (32)>
20-
// HT = <US-ASCII HT, horizontal-tab (9)>
21-
// <"> = <US-ASCII double-quote mark (34)>
22-
'!', '#', '$', '%', '&', "'", '*', '+', '-', '.', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
23-
'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
24-
'Y', 'Z', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
25-
'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '|', '~',
26-
);
27-
public static $RFC6265 = array(
28-
// RFC6265: "US-ASCII characters excluding CTLs, whitespace DQUOTE, comma, semicolon, and backslash".
29-
// %x21
30-
'!',
31-
// %x23-2B
32-
'#', '$', '%', '&', "'", '(', ')', '*', '+',
33-
// %x2D-3A
34-
'-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':',
35-
// %x3C-5B
36-
'<', '=', '>', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
37-
'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[',
38-
// %x5D-7E
39-
']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
40-
's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~',
41-
);
42-
4310
public $curl;
4411
public $id = null;
4512

@@ -81,6 +48,39 @@ class Curl
8148
private $xmlPattern = '~^(?:text/|application/(?:atom\+|rss\+)?)xml~i';
8249
private $defaultDecoder = null;
8350

51+
public static $RFC2616 = array(
52+
// RFC2616: "any CHAR except CTLs or separators".
53+
// CHAR = <any US-ASCII character (octets 0 - 127)>
54+
// CTL = <any US-ASCII control character
55+
// (octets 0 - 31) and DEL (127)>
56+
// separators = "(" | ")" | "<" | ">" | "@"
57+
// | "," | ";" | ":" | "\" | <">
58+
// | "/" | "[" | "]" | "?" | "="
59+
// | "{" | "}" | SP | HT
60+
// SP = <US-ASCII SP, space (32)>
61+
// HT = <US-ASCII HT, horizontal-tab (9)>
62+
// <"> = <US-ASCII double-quote mark (34)>
63+
'!', '#', '$', '%', '&', "'", '*', '+', '-', '.', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
64+
'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
65+
'Y', 'Z', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q',
66+
'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '|', '~',
67+
);
68+
public static $RFC6265 = array(
69+
// RFC6265: "US-ASCII characters excluding CTLs, whitespace DQUOTE, comma, semicolon, and backslash".
70+
// %x21
71+
'!',
72+
// %x23-2B
73+
'#', '$', '%', '&', "'", '(', ')', '*', '+',
74+
// %x2D-3A
75+
'-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':',
76+
// %x3C-5B
77+
'<', '=', '>', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q',
78+
'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '[',
79+
// %x5D-7E
80+
']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
81+
's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}', '~',
82+
);
83+
8484
private static $deferredProperties = array(
8585
'effectiveUrl',
8686
'rfc2616',
@@ -148,8 +148,8 @@ public function buildPostData($data)
148148
// Manually build a single-dimensional array from a multi-dimensional array as using curl_setopt($ch,
149149
// CURLOPT_POSTFIELDS, $data) doesn't correctly handle multi-dimensional arrays when files are
150150
// referenced.
151-
if (self::is_array_multidim($data)) {
152-
$data = self::array_flatten_multidim($data);
151+
if (\Curl\ArrayUtil::is_array_multidim($data)) {
152+
$data = \Curl\ArrayUtil::array_flatten_multidim($data);
153153
}
154154

155155
// Modify array values to ensure any referenced files are properly handled depending on the support of
@@ -1379,78 +1379,4 @@ private function parseResponseHeaders($raw_response_headers)
13791379
}
13801380
return $response_headers;
13811381
}
1382-
1383-
/**
1384-
* Is Array Assoc
1385-
*
1386-
* @access public
1387-
* @param $array
1388-
*
1389-
* @return boolean
1390-
*/
1391-
public static function is_array_assoc($array)
1392-
{
1393-
return (bool)count(array_filter(array_keys($array), 'is_string'));
1394-
}
1395-
1396-
/**
1397-
* Is Array Multidim
1398-
*
1399-
* @access public
1400-
* @param $array
1401-
*
1402-
* @return boolean
1403-
*/
1404-
public static function is_array_multidim($array)
1405-
{
1406-
if (!is_array($array)) {
1407-
return false;
1408-
}
1409-
1410-
return (bool)count(array_filter($array, 'is_array'));
1411-
}
1412-
1413-
/**
1414-
* Array Flatten Multidim
1415-
*
1416-
* @access public
1417-
* @param $array
1418-
* @param $prefix
1419-
*
1420-
* @return array
1421-
*/
1422-
public static function array_flatten_multidim($array, $prefix = false)
1423-
{
1424-
$return = array();
1425-
if (is_array($array) || is_object($array)) {
1426-
if (empty($array)) {
1427-
$return[$prefix] = '';
1428-
} else {
1429-
foreach ($array as $key => $value) {
1430-
if (is_scalar($value)) {
1431-
if ($prefix) {
1432-
$return[$prefix . '[' . $key . ']'] = $value;
1433-
} else {
1434-
$return[$key] = $value;
1435-
}
1436-
} else {
1437-
if ($value instanceof \CURLFile) {
1438-
$return[$key] = $value;
1439-
} else {
1440-
$return = array_merge(
1441-
$return,
1442-
self::array_flatten_multidim(
1443-
$value,
1444-
$prefix ? $prefix . '[' . $key . ']' : $key
1445-
)
1446-
);
1447-
}
1448-
}
1449-
}
1450-
}
1451-
} elseif ($array === null) {
1452-
$return[$prefix] = $array;
1453-
}
1454-
return $return;
1455-
}
14561382
}

src/Curl/MultiCurl.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ public function start()
641641
}
642642

643643
for ($i = 0; $i < $concurrency; $i++) {
644-
$this->initHandle(array_pop($this->curls));
644+
$this->initHandle(array_shift($this->curls));
645645
}
646646

647647
do {
@@ -662,7 +662,7 @@ public function start()
662662

663663
// Start a new request before removing the handle of the completed one.
664664
if (count($this->curls) >= 1) {
665-
$this->initHandle(array_pop($this->curls));
665+
$this->initHandle(array_shift($this->curls));
666666
}
667667
curl_multi_remove_handle($this->multiCurl, $ch->curl);
668668

0 commit comments

Comments
 (0)