-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclowder.php
122 lines (88 loc) · 3.18 KB
/
clowder.php
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
// ===========================================================================
//
// @name ......... : Clowder PHP Client
//
// @author ....... : Ouahib El Hanchi <[email protected]>
//
// ===========================================================================
// Clowder ===================================================================
class Clowder
{
// Class constantes ------------------------------------------------------
const API_URL = 'http://www.clowder.io/api';
// Attributes ------------------------------------------------------------
private $api_key;
private $ch;
// Constructor -----------------------------------------------------------
function __construct($api_key=null)
{
$this->ch = curl_init();
curl_setopt($this->ch, CURLOPT_POST, 1);
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($this->ch, CURLOPT_HEADER, 0);
$this->api_key = $api_key;
}
// Private methods -------------------------------------------------------
private function check($data)
{
if(!is_array($data)) {
throw InvalidArgumentException("Expected an array.");
}
if(array_key_exists('status', $data)) {
throw OutOfRangeException('Status should not be provided.');
}
}
// -----------------------------------------------------------------------
private function send($data)
{
if(!empty($this->api_key)) {
$data['api_key'] = $this->api_key;
}
if(!array_key_exists('value', $data)) {
$data['value'] = $data['status'];
}
if(array_key_exists('frequency', $data)) {
$data['frequency'] = $this->clean_frequency($data['frequency']);
}
$url = array_key_exists('url', $data) ? $data['url'] : self::API_URL;
curl_setopt($this->ch, CURLOPT_URL, $url);
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $data);
curl_exec($this->ch);
}
// -----------------------------------------------------------------------
private function clean_frequency($frequency)
{
if(is_int($frequency)) {
return $frequency;
} else if(is_object($frequency)) {
$date1 = new DateTime();
$date2 = new DateTime();
$date2->add($frequency);
return $date2->getTimestamp() - $date1->getTimestamp();
}
throw InvalidArgumentException("Invalid frequency.");
}
// Public methodss -------------------------------------------------------
public function ok($data)
{
$this->check($data);
$data['status'] = 1;
$this->send($data);
}
// -----------------------------------------------------------------------
public function fail($data)
{
$this->check($data);
$data['status'] = -1;
$this->send($data);
}
// Destructor ------------------------------------------------------------
function __destruct()
{
curl_close($this->ch);
}
}
// EOF =======================================================================
?>