[8c9fcb2] | 1 | <?php |
---|
| 2 | |
---|
| 3 | /* |
---|
[357352b] | 4 | * @function multiRequest. |
---|
| 5 | * @param URLs array (may include header and POST data), cURL options array. |
---|
| 6 | * @return Array of arrays with JSON requests and response codes. |
---|
| 7 | * @warning Does not verifies server certificate. |
---|
| 8 | * @Date 2015-10-14 |
---|
[8c9fcb2] | 9 | */ |
---|
[69052958] | 10 | function multiRequest($data, $options=array(CURLOPT_SSL_VERIFYHOST => false, CURLOPT_SSL_VERIFYPEER => false)) { |
---|
[8c9fcb2] | 11 | |
---|
| 12 | // array of curl handles |
---|
| 13 | $curly = array(); |
---|
[357352b] | 14 | // Data to be returned (response data and code) |
---|
[8c9fcb2] | 15 | $result = array(); |
---|
| 16 | |
---|
| 17 | // multi handle |
---|
| 18 | $mh = curl_multi_init(); |
---|
| 19 | |
---|
| 20 | // loop through $data and create curl handles |
---|
| 21 | // then add them to the multi-handle |
---|
| 22 | foreach ($data as $id => $d) { |
---|
| 23 | |
---|
| 24 | |
---|
| 25 | $curly[$id] = curl_init(); |
---|
| 26 | |
---|
| 27 | $url = (is_array($d) && !empty($d['url'])) ? $d['url'] : $d; |
---|
[ba3c59d] | 28 | curl_setopt($curly[$id], CURLOPT_URL, $url); |
---|
[357352b] | 29 | // HTTP headers? |
---|
[69052958] | 30 | if (is_array($d) && !empty($d['header'])) { |
---|
[e46f7ce] | 31 | curl_setopt($curly[$id], CURLOPT_HTTPHEADER, $d['header']); |
---|
[69052958] | 32 | } else { |
---|
| 33 | curl_setopt($curly[$id], CURLOPT_HEADER, 0); |
---|
| 34 | } |
---|
[8c9fcb2] | 35 | curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1); |
---|
[8083fb2] | 36 | curl_setopt($curly[$id], CURLOPT_TIMEOUT, 1); |
---|
[8c9fcb2] | 37 | |
---|
| 38 | // post? |
---|
| 39 | if (is_array($d)) { |
---|
| 40 | if (!empty($d['post'])) { |
---|
[ba3c59d] | 41 | curl_setopt($curly[$id], CURLOPT_POST, 1); |
---|
[8c9fcb2] | 42 | curl_setopt($curly[$id], CURLOPT_POSTFIELDS, $d['post']); |
---|
| 43 | } |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | // extra options? |
---|
| 47 | if (!empty($options)) { |
---|
| 48 | curl_setopt_array($curly[$id], $options); |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | curl_multi_add_handle($mh, $curly[$id]); |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | // execute the handles |
---|
| 55 | $running = null; |
---|
| 56 | do { |
---|
| 57 | curl_multi_exec($mh, $running); |
---|
| 58 | } while($running > 0); |
---|
| 59 | |
---|
| 60 | |
---|
[357352b] | 61 | // Get content and HTTP code, and remove handles |
---|
[8c9fcb2] | 62 | foreach($curly as $id => $c) { |
---|
[357352b] | 63 | $result[$id]['data'] = curl_multi_getcontent($c); |
---|
| 64 | $result[$id]['code'] = curl_getinfo($c, CURLINFO_HTTP_CODE); |
---|
[8c9fcb2] | 65 | curl_multi_remove_handle($mh, $c); |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | // all done |
---|
| 69 | curl_multi_close($mh); |
---|
| 70 | |
---|
| 71 | return $result; |
---|
| 72 | } |
---|
| 73 | |
---|
[15acccd] | 74 | |
---|
| 75 | |
---|
| 76 | /** |
---|
| 77 | * @brief Realiza una petición POST, PUT, GET, DELETE a una webservice. Pueden enviarse datos y cabeceras especificas |
---|
| 78 | * @param $method Metodo http (POST, GET, etc) |
---|
| 79 | * @param $url Url del webservice a consultar |
---|
| 80 | * @param $data array de datos a enviar. Ej. array("param" => "value") ==> index.php?param=value |
---|
| 81 | * @param $headers Cabeceras especificas de la peticion. Ej. array('Authorization: "9Ka7wG3EqhcjylUeQXITy0llj2TS8eKe"') |
---|
| 82 | */ |
---|
| 83 | // Method: POST, PUT, GET etc |
---|
| 84 | // Data: |
---|
| 85 | // Ej. callAPI("GET", "http://172.17.11.176/opengnsys/rest/index.php/repository/images?extensions[]=img&extensions[]=sum", array('Authorization: "9Ka7wG3EqhcjylUeQXITy0llj2TS8eKe"')) |
---|
| 86 | function callAPI($method, $url, $data = false, $headers = false) |
---|
| 87 | { |
---|
| 88 | $curl = curl_init(); |
---|
| 89 | |
---|
| 90 | switch ($method) |
---|
| 91 | { |
---|
| 92 | case "POST": |
---|
| 93 | curl_setopt($curl, CURLOPT_POST, 1); |
---|
| 94 | |
---|
| 95 | if ($data) |
---|
| 96 | curl_setopt($curl, CURLOPT_POSTFIELDS, $data); |
---|
| 97 | break; |
---|
| 98 | case "PUT": |
---|
| 99 | curl_setopt($curl, CURLOPT_PUT, 1); |
---|
| 100 | break; |
---|
| 101 | default: |
---|
| 102 | if ($data) |
---|
| 103 | $url = sprintf("%s?%s", $url, http_build_query($data)); |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | // Optional Authentication: |
---|
| 107 | //curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); |
---|
| 108 | //curl_setopt($curl, CURLOPT_USERPWD, "username:password"); |
---|
| 109 | |
---|
| 110 | curl_setopt($curl, CURLOPT_URL, $url); |
---|
| 111 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); |
---|
| 112 | if($headers != false){ |
---|
| 113 | curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | $result = curl_exec($curl); |
---|
| 117 | |
---|
| 118 | curl_close($curl); |
---|
| 119 | |
---|
| 120 | return $result; |
---|
| 121 | } |
---|
[a142b14] | 122 | ?> |
---|