-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPartclAPI.php
More file actions
105 lines (83 loc) · 2.47 KB
/
Copy pathPartclAPI.php
File metadata and controls
105 lines (83 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
class PartclAPI {
private static $publish_key = '';
private static $web_key = '';
public static function setPublishKey($key = null) {
if (empty($key)) return false;
self::$publish_key = $key;
}
public static function setWebKey($key = null) {
if (empty($key)) return false;
self::$web_key = $key;
}
public static function send($tag = null, $val) {
if (empty($tag)) return false;
if(is_array($val) ) {
$val = json_encode($val);
}
else {
$val = urlencode($val);
}
$url = 'http://partcl.com/publish?publish_key='. self::$publish_key .'&id='. $tag .'&value='. $val;
file_get_contents($url);
}
/*
@param Mixed $tag - tag code to fetch (one tag or array of tags)
@param Boolean $decodedValueAsJson - if set to true, automated decoding tag value as json string
@return Boolean|Mixed - tag value or boolean if error
!IMPORTANT! You must set your public web_key perviosly to fetch any data (see setWebKey function)
Examples:
PartclAPI::setWebKey('<your web_key>');
echo PartclAPI::get('srv:time.gmt'); // output e.g. 11:58:37:529
Multiple tag at once:
var_dump( PartclAPI::get(Array('srv:time.gmt','srv:all.today.messages')) );
*/
public static function get($tag = null, $decodedValueAsJson = false) {
if (empty($tag)) return false;
if (empty(self::$web_key)) return false;
$output_type = 'val'; //'array'
$_server = rand(1,2); //round server
$sessionId = rand() . '_' . rand(1, 99999);
$_tags = Array();
if (is_array($tag))
{
$output_type = 'array';
foreach($tag as $x)
{
$x = trim($x);
if ((!empty($x)) && (!in_array($x, $_tags)))
$_tags[] = trim($x);
}
}
else
$_tags[] = trim($tag);
$url = 'http://push'.$_server.'.partcl.com/poll?tags='.implode(',', $_tags).'&sessionId='.$sessionId.'&clientId='.self::$web_key.'&_='.microtime(true);
$_resp = file_get_contents($url);
try
{
$obj = json_decode($_resp, true);
$_return = Array();
if (($obj['status'] == 'OK') && (array_key_exists('data', $obj)))
{
foreach($_tags as $x)
{
if (array_key_exists($x, $obj['data']))
{
if ($decodedValueAsJson === true)
$_return[ $x ] = json_decode($obj['data'][ $x ], true);
else
$_return[ $x ] = $obj['data'][ $x ];
}
}
if ($output_type == 'array')
return $_return;
else
return array_shift($_return);
}
else
return false;
}catch(Exception $x){
return false;
}
}
}