|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tylercd100\Monolog\Handler; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Monolog\Logger; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Clickatell - Monolog Handler |
|
10
|
|
|
* @url https://www.clickatell.com/developers/api-documentation/rest-api-request-parameters |
|
11
|
|
|
*/ |
|
12
|
|
|
class ClickatellHandler extends SMSHandler |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* API version 1 |
|
16
|
|
|
*/ |
|
17
|
|
|
const API_V1 = '2010-04-01'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @param string $secret Twilio API Secret Token |
|
21
|
|
|
* @param string $fromNumber The phone number that will be shown as the sender ID |
|
22
|
|
|
* @param string $toNumber The phone number to which the message will be sent |
|
23
|
|
|
* @param int $level The minimum logging level at which this handler will be triggered |
|
24
|
|
|
* @param bool $bubble Whether the messages that are handled can bubble up the stack or not |
|
25
|
|
|
* @param bool $useSSL Whether to connect via SSL. |
|
26
|
|
|
* @param string $host The Twilio server hostname. |
|
27
|
|
|
* @param string $version The Twilio API version (default ClickatellHandler::API_V1) |
|
28
|
|
|
* @param string $limit The character limit |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct($secret, $fromNumber, $toNumber, $level = Logger::CRITICAL, $bubble = true, $useSSL = true, $host = 'platform.clickatell.com', $version = self::API_V1, $limit = 160) |
|
31
|
|
|
{ |
|
32
|
|
|
if($version !== self::API_V1){ |
|
33
|
|
|
throw new Exception('API Version \'{$version}\' is not supported!'); |
|
34
|
|
|
} |
|
35
|
|
|
parent::__construct($secret, null, $fromNumber, $toNumber, $level, $bubble, $useSSL, $host, $version, $limit); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* {@inheritdoc} |
|
40
|
|
|
* |
|
41
|
|
|
* @param array $record |
|
42
|
|
|
* @return string |
|
43
|
|
|
*/ |
|
44
|
|
|
protected function buildContent($record) |
|
45
|
|
|
{ |
|
46
|
|
|
if(strlen($record['formatted']) > $this->limit){ |
|
47
|
|
|
$record['formatted'] = substr($record['formatted'], 0, $this->limit); |
|
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$dataArray = [ |
|
51
|
|
|
'content' => $record['formatted'], |
|
52
|
|
|
'to' => (!is_array($this->toNumber)? [$this->toNumber] : $this->toNumber) |
|
53
|
|
|
]; |
|
54
|
|
|
|
|
55
|
|
|
($this->fromNumber)? $dataArray["from"] = $this->fromNumber : false; |
|
56
|
|
|
|
|
57
|
|
|
return json_encode($dataArray); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Builds the URL for the API call |
|
62
|
|
|
* |
|
63
|
|
|
* @return string |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function buildRequestUrl() |
|
66
|
|
|
{ |
|
67
|
|
|
return "POST /messages HTTP/1.1\r\n"; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: