PluginProbe
Webling / 2.0
Webling v2.0
trunk 2.0 3.0 3.0.1 3.0.2 3.0.3 3.1.0 3.1.1 3.1.2 3.2.0 3.2.1 3.3.0 3.4.0 3.4.1 3.5.0 3.6.0 3.7.0 3.7.1 3.7.2 3.7.3 3.8.0 3.9.0 3.9.1 3.9.2
webling / WeblingAPI.php

WeblingAPI.php in Webling 2.0, at WeblingAPI.php

100 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class WeblingAPI {
4
5 private $API_PREFIX = '/api/1/';
6
7 private $WEBLING_DOMAIN = null;
8 private $WEBLING_APIKEY = null;
9
10 private $connection_status = null;
11 private $last_request_info = null;
12
13 function __construct($domain, $apikey) {
14 $this->WEBLING_DOMAIN = $domain;
15 $this->WEBLING_APIKEY = $apikey;
16 }
17
18 public function get($path){
19 return $this->httpRequest($path);
20 }
21
22 public function getConnectionStatus(){
23 if($this->connection_status === null){
24 if(!$this->WEBLING_DOMAIN or !$this->WEBLING_APIKEY){
25 $this->connection_status = -2;
26 } else {
27 $config = $this->httpRequest('config');
28 if($this->last_request_info !== null){
29 $this->connection_status = $this->last_request_info['http_code'];
30 } else {
31 $this->connection_status = 1;
32 }
33 }
34 }
35 return $this->connection_status;
36 }
37
38 public function isConnected(){
39 if($this->getConnectionStatus() == 200){
40 return true;
41 }
42 return false;
43 }
44
45 public function getLastRequestStatusCode(){
46 if($this->last_request_info !== null){
47 return $this->last_request_info['http_code'];
48 }
49 return 0;
50 }
51
52 public function messageFromReturnCode($code){
53 switch ($code){
54 case -3:
55 return 'Fehler bei der CURL Abfrage.';
56 break;
57 case -2:
58 return 'Zugangsdaten nicht vollständig. Gib "Webling-URL" und "API Key" an.';
59 break;
60 case 0:
61 return 'Verbindung zu Webling fehlgeschlagen. Überprüfe die Webling-URL.';
62 break;
63 case 200:
64 return 'Verbindung erfolgreich';
65 break;
66 case 401:
67 return 'Ungültiger API Key';
68 break;
69 case 404:
70 return 'Ungültige Webling-URL';
71 break;
72 default:
73 return 'Fehlercode: '.$code;
74 }
75 }
76
77 private function httpRequest($path, $timeout = 15){
78 try {
79 $curl = curl_init();
80 curl_setopt($curl, CURLOPT_URL, $this->WEBLING_DOMAIN . $this->API_PREFIX . $path . "?apikey=" . $this->WEBLING_APIKEY);
81 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
82 curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
83 curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
84 $conn = curl_exec($curl);
85 if($conn !== false) {
86 $response = json_decode($conn, true);
87 } else {
88 $response = null;
89 }
90 $this->last_request_info = curl_getinfo($curl);
91 curl_close($curl);
92 return $response;
93 } catch (Exception $e){
94 if($this->connection_status === null) {
95 $this->connection_status = -3;
96 }
97 return null;
98 }
99 }
100 }