-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmly_requests.php
80 lines (64 loc) · 2.11 KB
/
smly_requests.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
<?php
class smly
{
private $username;
private $password;
private $domain;
public $errors = array();
private $protocol = 'https';
private $tld = 'net';
public function __construct($username, $password, $domain) {
$this->username = $username;
$this->password = $password;
$this->domain = $this->protocol . '://' . $domain . '.sendsmaily.' . $this->tld . '/api/';
}
public function curl_get($url, $query = array()) {
$query = urldecode(http_build_query($query));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->domain . $url . '?' . $query);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, $this->username . ':' . $this->password);
$result = curl_exec($ch);
if ($result === false) {
$this->_error(curl_error($ch));
}
curl_close($ch);
return $this->_process_request($result);
}
public function curl_post($url, $query) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->domain . $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($query));
curl_setopt($ch, CURLOPT_USERPWD, $this->username . ':' . $this->password);
$result = curl_exec($ch);
if ($result === false) {
$this->_error(curl_error($ch));
}
curl_close($ch);
$result = $this->_process_request($result);
if (!isset($result['code'])) {
$this->_error('Something went wrong with the request.');
return FALSE;
}
elseif ((int) $result['code'] === 101) {
return TRUE;
}
else {
$this->_error($result['message']);
return FALSE;
}
}
private function _process_request($curl_result) {
return json_decode($curl_result, true);
}
private function _error($msg) {
$this->errors[] = $this->domain . ' - ' . date('d.m.Y H:i:s') . ': ' . $msg;
}
public function set_domain($domain) {
$this->domain = $this->protocol . '://' . $domain . '.sendsmaily.' . $this->tld . '/api/';
}
}