EGOCMS  18.0
EGOTEC Content-Managament-System
Ego_REST_Client.php
gehe zur Dokumentation dieser Datei
1 <?php
18  const TIMEOUT = 30;
19 
25  private $url;
26 
32  private $session;
33 
41  public function __construct($url = '', $user_id = '', $token = '') {
42  $this->url = $url;
43  if (!empty($user_id)) {
44  $this->start($user_id, $token);
45  }
46  }
47 
55  public function start($user_id, $token) {
56  return $this->post('startSession', array($user_id, $token));
57  }
58 
64  public function close() {
65  if (!empty($this->session)) {
66  $this->post('closeSession');
67  }
68  }
69 
78  public function get($path = '', $params = array()) {
79  return $this->send($path, $params, 'GET');
80  }
81 
90  public function put($path = '', $params = array()) {
91  return $this->send($path, $params, 'PUT');
92  }
93 
102  public function post($path = '', $params = array()) {
103  return $this->send($path, $params, 'POST');
104  }
105 
114  public function delete($path = '', $params = array()) {
115  return $this->send($path, $params, 'DELETE');
116  }
117 
127  private function send($path = '', $params = array(), $method = 'GET') {
128  $url = $this->url.$path;
129  $ch = curl_init();
130 
131  switch ($method) {
132  case 'GET':
133  curl_setopt($ch, CURLOPT_HTTPGET, true);
134  if (!empty($params)) {
135  $url .= (strpos($url, '?') !== false ? '&' : '?').http_build_query($params);
136  }
137  break;
138  case 'PUT':
139  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
140  if (!empty($params['params']['@file'])) {
141  // Datei übertragen
142  curl_setopt($ch, CURLOPT_INFILE, $params['params']['@file']);
143  curl_setopt($ch, CURLOPT_INFILESIZE, filesize($params['params']['@file']));
144  unset($params['params']['@file']);
145  }
146  curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
147  break;
148  case 'POST':
149  curl_setopt($ch, CURLOPT_POST, true);
150  curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
151  break;
152  case 'DELETE':
153  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
154  curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
155  }
156 
157  // Session ID übermitteln
158  if (!empty($this->session)) {
159  curl_setopt($ch, CURLOPT_COOKIE, EGOTEC."={$this->session}");
160  }
161 
162  curl_setopt($ch, CURLOPT_URL, $url);
163  curl_setopt($ch, CURLOPT_HEADER, true);
164  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
165  curl_setopt($ch, CURLOPT_TIMEOUT, self::TIMEOUT);
166 
167  if (!empty($GLOBALS['egotec_conf']['proxy'])) {
168  curl_setopt($ch, CURLOPT_PROXY, $GLOBALS['egotec_conf']['proxy']['proxy_host']);
169  curl_setopt($ch, CURLOPT_PROXYPORT, $GLOBALS['egotec_conf']['proxy']['proxy_port']);
170  if (!empty($GLOBALS['egotec_conf']['proxy']['proxy_ssl'])) {
171  curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, true);
172  }
173  if (!empty($GLOBALS['egotec_conf']['proxy']['proxy_login'])) {
174  curl_setopt($ch, CURLOPT_PROXYAUTH, implode(':', array(
175  $GLOBALS['egotec_conf']['proxy']['proxy_login'],
176  (string) $GLOBALS['egotec_conf']['proxy']['proxy_password']
177  )));
178  }
179  }
180 
181  $result = curl_exec($ch);
182 
183  $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
184  $header = substr($result, 0, $header_size);
185  $result = substr($result, $header_size);
186 
187  // Die Session ID aus dem Header ermitteln
188  if (!empty($header) && preg_match('/Set-Cookie: ?'.preg_quote(EGOTEC, '/').'=(.*?);/msi', $header, $match)) {
189  $this->session = $match[1];
190  }
191 
192  curl_close($ch);
193  return json_decode($result, true);
194  }
195 }
196 ?>
__construct($url='', $user_id='', $token='')
put($path='', $params=array())
start($user_id, $token)
post($path='', $params=array())