source: admin/WebConsole/includes/restfunctions.php @ 8ff4bd5

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 8ff4bd5 was 04319fb, checked in by ramon <ramongomez@…>, 8 years ago

#799: Bajar el timeout de conexión REST a 200 ms.

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

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