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(CURLOPT_SSL_VERIFYHOST => false, CURLOPT_SSL_VERIFYPEER => false)) { |
---|
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 | if (is_array($d) && !empty($d['header'])) { |
---|
29 | curl_setopt($curly[$id], CURLOPT_HTTPHEADER, $d['header']); |
---|
30 | } else { |
---|
31 | curl_setopt($curly[$id], CURLOPT_HEADER, 0); |
---|
32 | } |
---|
33 | curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1); |
---|
34 | curl_setopt($curly[$id], CURLOPT_TIMEOUT, 1); |
---|
35 | |
---|
36 | // post? |
---|
37 | if (is_array($d)) { |
---|
38 | if (!empty($d['post'])) { |
---|
39 | curl_setopt($curly[$id], CURLOPT_POST, 1); |
---|
40 | curl_setopt($curly[$id], CURLOPT_POSTFIELDS, $d['post']); |
---|
41 | } |
---|
42 | } |
---|
43 | |
---|
44 | // extra options? |
---|
45 | if (!empty($options)) { |
---|
46 | curl_setopt_array($curly[$id], $options); |
---|
47 | } |
---|
48 | |
---|
49 | curl_multi_add_handle($mh, $curly[$id]); |
---|
50 | } |
---|
51 | |
---|
52 | // execute the handles |
---|
53 | $running = null; |
---|
54 | do { |
---|
55 | curl_multi_exec($mh, $running); |
---|
56 | } while($running > 0); |
---|
57 | |
---|
58 | |
---|
59 | // get content and remove handles |
---|
60 | foreach($curly as $id => $c) { |
---|
61 | $result[$id] = curl_multi_getcontent($c); |
---|
62 | curl_multi_remove_handle($mh, $c); |
---|
63 | } |
---|
64 | |
---|
65 | // all done |
---|
66 | curl_multi_close($mh); |
---|
67 | |
---|
68 | return $result; |
---|
69 | } |
---|
70 | |
---|
71 | |
---|
72 | |
---|
73 | /** |
---|
74 | * @brief Realiza una petición POST, PUT, GET, DELETE a una webservice. Pueden enviarse datos y cabeceras especificas |
---|
75 | * @param $method Metodo http (POST, GET, etc) |
---|
76 | * @param $url Url del webservice a consultar |
---|
77 | * @param $data array de datos a enviar. Ej. array("param" => "value") ==> index.php?param=value |
---|
78 | * @param $headers Cabeceras especificas de la peticion. Ej. array('Authorization: "9Ka7wG3EqhcjylUeQXITy0llj2TS8eKe"') |
---|
79 | */ |
---|
80 | // Method: POST, PUT, GET etc |
---|
81 | // Data: |
---|
82 | // Ej. callAPI("GET", "http://172.17.11.176/opengnsys/rest/index.php/repository/images?extensions[]=img&extensions[]=sum", array('Authorization: "9Ka7wG3EqhcjylUeQXITy0llj2TS8eKe"')) |
---|
83 | function callAPI($method, $url, $data = false, $headers = false) |
---|
84 | { |
---|
85 | $curl = curl_init(); |
---|
86 | |
---|
87 | switch ($method) |
---|
88 | { |
---|
89 | case "POST": |
---|
90 | curl_setopt($curl, CURLOPT_POST, 1); |
---|
91 | |
---|
92 | if ($data) |
---|
93 | curl_setopt($curl, CURLOPT_POSTFIELDS, $data); |
---|
94 | break; |
---|
95 | case "PUT": |
---|
96 | curl_setopt($curl, CURLOPT_PUT, 1); |
---|
97 | break; |
---|
98 | default: |
---|
99 | if ($data) |
---|
100 | $url = sprintf("%s?%s", $url, http_build_query($data)); |
---|
101 | } |
---|
102 | |
---|
103 | // Optional Authentication: |
---|
104 | //curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); |
---|
105 | //curl_setopt($curl, CURLOPT_USERPWD, "username:password"); |
---|
106 | |
---|
107 | curl_setopt($curl, CURLOPT_URL, $url); |
---|
108 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); |
---|
109 | if($headers != false){ |
---|
110 | curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); |
---|
111 | } |
---|
112 | |
---|
113 | $result = curl_exec($curl); |
---|
114 | |
---|
115 | curl_close($curl); |
---|
116 | |
---|
117 | return $result; |
---|
118 | } |
---|
119 | ?> |
---|