1<?php declare(strict_types=1); 2 3/* 4 * This file is part of the Monolog package. 5 * 6 * (c) Jordi Boggiano <j.boggiano@seld.be> 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12namespace Monolog\Handler; 13 14use Monolog\Logger; 15use Monolog\Utils; 16 17/** 18 * IFTTTHandler uses cURL to trigger IFTTT Maker actions 19 * 20 * Register a secret key and trigger/event name at https://ifttt.com/maker 21 * 22 * value1 will be the channel from monolog's Logger constructor, 23 * value2 will be the level name (ERROR, WARNING, ..) 24 * value3 will be the log record's message 25 * 26 * @author Nehal Patel <nehal@nehalpatel.me> 27 */ 28class IFTTTHandler extends AbstractProcessingHandler 29{ 30 /** @var string */ 31 private $eventName; 32 /** @var string */ 33 private $secretKey; 34 35 /** 36 * @param string $eventName The name of the IFTTT Maker event that should be triggered 37 * @param string $secretKey A valid IFTTT secret key 38 */ 39 public function __construct(string $eventName, string $secretKey, $level = Logger::ERROR, bool $bubble = true) 40 { 41 if (!extension_loaded('curl')) { 42 throw new MissingExtensionException('The curl extension is needed to use the IFTTTHandler'); 43 } 44 45 $this->eventName = $eventName; 46 $this->secretKey = $secretKey; 47 48 parent::__construct($level, $bubble); 49 } 50 51 /** 52 * {@inheritDoc} 53 */ 54 public function write(array $record): void 55 { 56 $postData = [ 57 "value1" => $record["channel"], 58 "value2" => $record["level_name"], 59 "value3" => $record["message"], 60 ]; 61 $postString = Utils::jsonEncode($postData); 62 63 $ch = curl_init(); 64 curl_setopt($ch, CURLOPT_URL, "https://maker.ifttt.com/trigger/" . $this->eventName . "/with/key/" . $this->secretKey); 65 curl_setopt($ch, CURLOPT_POST, true); 66 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 67 curl_setopt($ch, CURLOPT_POSTFIELDS, $postString); 68 curl_setopt($ch, CURLOPT_HTTPHEADER, [ 69 "Content-Type: application/json", 70 ]); 71 72 Curl\Util::execute($ch); 73 } 74} 75