918-git-images-111dconfigfileconfigure-oglivegit-imageslgromero-new-oglivemainmaint-cronmount-efivarfsmultivmmultivm-ogboot-installerogClonningEngineogboot-installer-jenkinsoglive-ipv6test-python-scriptsticket-301ticket-50ticket-50-oldticket-577ticket-585ticket-611ticket-612ticket-693ticket-700ubu24tplunification2use-local-agent-oglivevarios-instalacionwebconsole3
Rev | Line | |
---|
[8c9fcb2] | 1 | <?php |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | * Function: multiRequest. |
---|
| 5 | * Params: urls array, cURL options array. |
---|
| 6 | * Returns: array with JSON requests. |
---|
| 7 | * Date: 2015-10-14 |
---|
| 8 | */ |
---|
| 9 | function multiRequest($data, $options = array()) { |
---|
| 10 | |
---|
| 11 | // array of curl handles |
---|
| 12 | $curly = array(); |
---|
| 13 | // data to be returned |
---|
| 14 | $result = array(); |
---|
| 15 | |
---|
| 16 | // multi handle |
---|
| 17 | $mh = curl_multi_init(); |
---|
| 18 | |
---|
| 19 | // loop through $data and create curl handles |
---|
| 20 | // then add them to the multi-handle |
---|
| 21 | foreach ($data as $id => $d) { |
---|
| 22 | |
---|
| 23 | |
---|
| 24 | $curly[$id] = curl_init(); |
---|
| 25 | |
---|
| 26 | $url = (is_array($d) && !empty($d['url'])) ? $d['url'] : $d; |
---|
| 27 | curl_setopt($curly[$id], CURLOPT_URL, $url); |
---|
| 28 | curl_setopt($curly[$id], CURLOPT_HEADER, 0); |
---|
| 29 | curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1); |
---|
| 30 | |
---|
| 31 | // post? |
---|
| 32 | if (is_array($d)) { |
---|
| 33 | if (!empty($d['post'])) { |
---|
| 34 | curl_setopt($curly[$id], CURLOPT_POST, 1); |
---|
| 35 | curl_setopt($curly[$id], CURLOPT_POSTFIELDS, $d['post']); |
---|
| 36 | } |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | // extra options? |
---|
| 40 | if (!empty($options)) { |
---|
| 41 | curl_setopt_array($curly[$id], $options); |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | curl_multi_add_handle($mh, $curly[$id]); |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | // execute the handles |
---|
| 48 | $running = null; |
---|
| 49 | do { |
---|
| 50 | curl_multi_exec($mh, $running); |
---|
| 51 | } while($running > 0); |
---|
| 52 | |
---|
| 53 | |
---|
| 54 | // get content and remove handles |
---|
| 55 | foreach($curly as $id => $c) { |
---|
| 56 | $result[$id] = curl_multi_getcontent($c); |
---|
| 57 | curl_multi_remove_handle($mh, $c); |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | // all done |
---|
| 61 | curl_multi_close($mh); |
---|
| 62 | |
---|
| 63 | return $result; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | ?> |
---|
| 67 | |
---|
Note: See
TracBrowser
for help on using the repository browser.