EGOCMS  24.0
EGOTEC Content-Managament-System
Ego_REST_Client.php
gehe zur Dokumentation dieser Datei
1 <?php
18  private $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 
54  public function setTimeout($n) {
55  $this->timeout = $n;
56  }
57 
65  public function start($user_id, $token) {
66  return $this->post('startSession', array($user_id, $token));
67  }
68 
74  public function close() {
75  if (!empty($this->session)) {
76  $this->post('closeSession');
77  }
78  }
79 
89  public function get($path = '', $params = array(), $header = array()) {
90  return $this->send($path, $params, 'GET', $header);
91  }
92 
102  public function put($path = '', $params = array(), $header = array()) {
103  return $this->send($path, $params, 'PUT', $header);
104  }
105 
115  public function post($path = '', $params = array(), $header = array()) {
116  return $this->send($path, $params, 'POST', $header);
117  }
118 
128  public function delete($path = '', $params = array(), $header = array()) {
129  return $this->send($path, $params, 'DELETE', $header);
130  }
131 
142  private function send($path = '', $params = array(), $method = 'GET', $header = array()) {
143  $url = $this->url.$path;
144  $ch = curl_init();
145 
146  // Zusätzliche Header setzen
147  if (!empty($header)) {
148  curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
149  }
150 
151  switch ($method) {
152  case 'GET':
153  curl_setopt($ch, CURLOPT_HTTPGET, true);
154  if (!empty($params)) {
155  $url .= (strpos($url, '?') !== false ? '&' : '?').json_encode($params);
156  }
157  break;
158  case 'PUT':
159  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
160  curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
161  break;
162  case 'POST':
163  curl_setopt($ch, CURLOPT_POST, true);
164  curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
165  break;
166  case 'DELETE':
167  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
168  curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
169  }
170 
171  // Session ID übermitteln
172  if (!empty($this->session)) {
173  curl_setopt($ch, CURLOPT_COOKIE, EGOTEC."={$this->session}");
174  }
175 
176  curl_setopt($ch, CURLOPT_URL, $url);
177  curl_setopt($ch, CURLOPT_HEADER, true);
178  //curl_setopt($ch, CURLOPT_VERBOSE, true); // verbose echo
179  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
180  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
181  curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
182 
183  if (!empty($GLOBALS['egotec_conf']['proxy'])) {
184  curl_setopt($ch, CURLOPT_PROXY, $GLOBALS['egotec_conf']['proxy']['proxy_host']);
185  curl_setopt($ch, CURLOPT_PROXYPORT, $GLOBALS['egotec_conf']['proxy']['proxy_port']);
186  if (!empty($GLOBALS['egotec_conf']['proxy']['proxy_ssl'])) {
187  curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, true);
188  }
189  if (!empty($GLOBALS['egotec_conf']['proxy']['proxy_login'])) {
190  curl_setopt($ch, CURLOPT_PROXYAUTH, implode(':', array(
191  $GLOBALS['egotec_conf']['proxy']['proxy_login'],
192  (string) $GLOBALS['egotec_conf']['proxy']['proxy_password']
193  )));
194  }
195  }
196 
197  $result = curl_exec($ch);
198 
199  $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
200  $header = substr($result, 0, $header_size);
201  $result = substr($result, $header_size);
202 
203  // Die Session ID aus dem Header ermitteln
204  if (!empty($header) && preg_match('/Set-Cookie: ?'.preg_quote(EGOTEC, '/').'=(.*?);/msi', $header, $match)) {
205  $this->session = $match[1];
206  }
207 
208  curl_close($ch);
209  return json_decode($result, true);
210  }
211 }
put($path='', $params=array(), $header=array())
start($user_id, $token)
__construct($url='', $user_id='', $token='')
post($path='', $params=array(), $header=array())