Skip to content

Commit ca0ac15

Browse files
committed
Init
0 parents  commit ca0ac15

File tree

4 files changed

+393
-0
lines changed

4 files changed

+393
-0
lines changed

.gitignore

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# phpstorm project files
2+
.idea
3+
4+
# netbeans project files
5+
nbproject
6+
7+
# zend studio for eclipse project files
8+
.buildpath
9+
.project
10+
.settings
11+
12+
# windows thumbnail cache
13+
Thumbs.db
14+
15+
# composer vendor dir
16+
/vendor
17+
18+
# composer itself is not needed
19+
composer.phar
20+
21+
# Mac DS_Store Files
22+
.DS_Store
23+
24+
# phpunit itself is not needed
25+
phpunit.phar
26+
# local phpunit config
27+
/phpunit.xml

composer.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "splynx/splynx-php-api",
3+
"type": "library",
4+
"description": "Splynx PHP Api class",
5+
"keywords": [
6+
"splynx"
7+
],
8+
"homepage": "https://bitbucket.org/splynx/splynx-php-api",
9+
"authors": [
10+
{
11+
"name": "Ruslan Malymon",
12+
"email": "[email protected]",
13+
"role": "Lead Developer"
14+
},
15+
{
16+
"name": "Oleksii Fedoryshyn",
17+
"email": "[email protected]",
18+
"role": "Developer"
19+
}
20+
],
21+
"autoload": {
22+
"psr-0": {
23+
"SplynxApi": "src/"
24+
}
25+
},
26+
"require": {
27+
"php": ">=5.3.0"
28+
}
29+
}

example/example.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
/**
3+
* Splynx API v.1.0 demo script
4+
* Author: Ruslan Malymon (Top Net Media s.r.o.)
5+
* https://splynx.com/wiki/index.php/API - documentation
6+
*/
7+
8+
include '../src/SplynxApi.php';
9+
10+
$api_url = 'http://splynx/'; // please set your Splynx URL
11+
12+
$key = "API_KEY"; // please set your key
13+
$secret = "API_SECRET"; // please set your secret
14+
15+
// don't forget to add permissions to API Key, for changing locations.
16+
17+
$api = new SplynxAPI($api_url, $key, $secret);
18+
19+
$locationsApiUrl = "admin/administration/locations";
20+
21+
print "<pre>";
22+
23+
print "List locations\n";
24+
$result = $api->api_call_get($locationsApiUrl);
25+
print "Result: ";
26+
if ($result) {
27+
print "Ok!\n";
28+
print_r($api->response);
29+
} else {
30+
print "Fail! Error code: $api->response_code\n";
31+
print_r($api->response);
32+
}
33+
print "\n-------------------------------------------------\n";
34+
35+
print "Create location\n";
36+
$result = $api->api_call_post($locationsApiUrl,
37+
array(
38+
'name' => 'API test #' . rand()
39+
));
40+
41+
print "Result: ";
42+
if ($result) {
43+
print "Ok!\n";
44+
print_r($api->response);
45+
$locationId = $api->response['id'];
46+
} else {
47+
print "Fail! Error code: $api->response_code\n";
48+
print_r($api->response);
49+
$locationId = false;
50+
}
51+
print "\n-------------------------------------------------\n";
52+
53+
if ($locationId) {
54+
55+
print "Retrieve location " . $locationId . "\n";
56+
$result = $api->api_call_get($locationsApiUrl, $locationId);
57+
print "Result: ";
58+
if ($result) {
59+
print "Ok!\n";
60+
print_r($api->response);
61+
} else {
62+
print "Fail! Error code: $api->response_code\n";
63+
print_r($api->response);
64+
}
65+
print "\n-------------------------------------------------\n";
66+
67+
68+
print "Change created location name\n";
69+
$result = $api->api_call_put($locationsApiUrl, $locationId, array('name' => 'NAME CHANGED #' . mt_rand()));
70+
print "Result: ";
71+
if ($result) {
72+
print "Ok!\n";
73+
print_r($api->response);
74+
} else {
75+
print "Fail! Error code: $api->response_code\n";
76+
print_r($api->response);
77+
}
78+
print "\n-------------------------------------------------\n";
79+
80+
print "Retrieve updated info\n";
81+
$result = $api->api_call_get($locationsApiUrl, $locationId);
82+
print "Result: ";
83+
if ($result) {
84+
print "Ok!\n";
85+
print_r($api->response);
86+
} else {
87+
print "Fail! Error code: $api->response_code\n";
88+
print_r($api->response);
89+
}
90+
print "\n-------------------------------------------------\n";
91+
92+
print "Delete created location\n";
93+
$result = $api->api_call_delete($locationsApiUrl, $locationId);
94+
print "Result: ";
95+
if ($result) {
96+
print "Ok!\n";
97+
print_r($api->response);
98+
} else {
99+
print "Fail! Error code: $api->response_code\n";
100+
print_r($api->response);
101+
}
102+
print "\n-------------------------------------------------\n";
103+
104+
}

src/SplynxApi.php

Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
<?php
2+
/**
3+
* Splynx API v. 1.0
4+
* REST API Class
5+
* Author: Ruslan Malymon (Top Net Media s.r.o.)
6+
* https://splynx.com/wiki/index.php/API - documentation
7+
*/
8+
9+
class SplynxApi
10+
{
11+
private $api_key;
12+
private $api_secret;
13+
private $nonce_v;
14+
private $url;
15+
private $version = '1.0';
16+
17+
public $debug = false;
18+
19+
public $result;
20+
public $response;
21+
public $response_code;
22+
23+
/**
24+
* Create Splynx API object
25+
*
26+
* @param $url
27+
* @param $api_key
28+
* @param $api_secret
29+
*/
30+
public function __construct($url, $api_key, $api_secret)
31+
{
32+
$this->url = $url . 'api/' . $this->version;
33+
$this->api_key = $api_key;
34+
$this->api_secret = $api_secret;
35+
$this->nonce();
36+
}
37+
38+
/**
39+
* Create signature for API call validation
40+
* @return string hash
41+
*/
42+
private function signature()
43+
{
44+
// Create string
45+
$string = $this->nonce_v . $this->api_key;
46+
47+
// Create hash
48+
$hash = hash_hmac('sha256', $string, $this->api_secret);
49+
$hash = strtoupper($hash);
50+
51+
return $hash;
52+
}
53+
54+
/**
55+
* Set nonce as timestamp
56+
*/
57+
private function nonce()
58+
{
59+
$this->nonce_v = round(microtime(true) * 100);
60+
}
61+
62+
/**
63+
* Send curl request to Splynx API
64+
*
65+
* @param string $method Method: get, delete, put, post
66+
* @param string $url
67+
* @param array $param
68+
* @return array JSON results
69+
*/
70+
private function curl_process($method, $url, $param = array())
71+
{
72+
$ch = curl_init();
73+
74+
if ($this->debug == true) {
75+
print $method . " to " . $url . "\n";
76+
print_r($param);
77+
}
78+
79+
$headers = array();
80+
$headers[] = 'Content-type: application/json';
81+
$auth_str = $this->make_auth();
82+
$headers[] = 'Authorization: Splynx-EA (' . $auth_str . ')';
83+
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
84+
85+
if ($method == 'DELETE') {
86+
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
87+
}
88+
89+
if ($method == 'POST') {
90+
curl_setopt($ch, CURLOPT_POST, true);
91+
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($param));
92+
}
93+
94+
if ($method == 'PUT') {
95+
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
96+
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($param));
97+
}
98+
99+
curl_setopt($ch, CURLOPT_URL, $url);
100+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
101+
curl_setopt($ch, CURLOPT_USERAGENT, 'Splynx PHP API ' . $this->version);
102+
103+
if ($this->debug == true) {
104+
curl_setopt($ch, CURLOPT_VERBOSE, 1);
105+
// curl_setopt($ch, CURLOPT_HEADER, 0);
106+
}
107+
108+
$out = curl_exec($ch);
109+
110+
if (curl_errno($ch)) {
111+
trigger_error("cURL failed. Error #" . curl_errno($ch) . ": " . curl_error($ch), E_USER_ERROR);
112+
}
113+
114+
$this->response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
115+
116+
curl_close($ch);
117+
118+
if ($this->debug == true) {
119+
var_dump($out);
120+
}
121+
122+
$this->result = false;
123+
124+
switch ($method) {
125+
case 'POST':
126+
if ($this->response_code == 201) {
127+
$this->result = true;
128+
}
129+
break;
130+
131+
case 'PUT':
132+
if ($this->response_code == 202) {
133+
$this->result = true;
134+
}
135+
break;
136+
137+
case 'DELETE':
138+
if ($this->response_code == 204) {
139+
$this->result = true;
140+
}
141+
break;
142+
143+
default:
144+
if ($this->response_code == 200) {
145+
$this->result = true;
146+
}
147+
break;
148+
}
149+
150+
$this->response = json_decode($out, true);
151+
152+
return $this->result;
153+
}
154+
155+
/**
156+
* Make Splynx Extended Authorization string
157+
*
158+
* @return string of Splynx EA
159+
*/
160+
private function make_auth()
161+
{
162+
$auth = array(
163+
'key' => $this->api_key,
164+
'signature' => $this->signature(),
165+
'nonce' => $this->nonce_v++
166+
);
167+
168+
return http_build_query($auth);
169+
}
170+
171+
private function getUrl($path, $id = null)
172+
{
173+
$url = $this->url . '/' . $path;
174+
if ($id !== null) {
175+
$url .= '/' . $id;
176+
}
177+
return $url;
178+
}
179+
180+
/**
181+
* Send API call GET to Splynx API
182+
*
183+
* @param $path
184+
* @param string $id
185+
* @return array
186+
*/
187+
public function api_call_get($path, $id = null)
188+
{
189+
return $this->curl_process('GET', $this->getUrl($path, $id));
190+
}
191+
192+
/**
193+
* Send API call DELETE to Splynx API
194+
*
195+
* @param string $path
196+
* @param integer $id
197+
* @return array JSON results
198+
*/
199+
public function api_call_delete($path, $id)
200+
{
201+
if (empty($id)) return false;
202+
return $this->curl_process('DELETE', $this->getUrl($path, $id));
203+
}
204+
205+
/**
206+
* Send API call POST (add) to Splynx API
207+
*
208+
* @param string $path
209+
* @param array $params
210+
* @return array JSON results
211+
*/
212+
public function api_call_post($path, $params)
213+
{
214+
if (empty($params)) return false;
215+
return $this->curl_process('POST', $this->getUrl($path), $params);
216+
}
217+
218+
/**
219+
* Send API call PUT (update) to Splynx API
220+
*
221+
* @param string $path
222+
* @param integer $id
223+
* @param array $params
224+
* @return array JSON results
225+
*/
226+
public function api_call_put($path, $id, $params)
227+
{
228+
if (empty($params)) return false;
229+
if (empty($id)) return false;
230+
return $this->curl_process('PUT', $this->getUrl($path, $id), $params);
231+
}
232+
233+
}

0 commit comments

Comments
 (0)