source: admin/WebConsole/includes/restfunctions.php @ 74cd321

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
Last change on this file since 74cd321 was a142b14, checked in by ramon <ramongomez@…>, 9 years ago

#743: Corregir errata de revisión r4904: falta fin de código PHP.

git-svn-id: https://opengnsys.es/svn/branches/version1.1@4905 a21b9725-9963-47de-94b9-378ad31fedc9

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