From e3ed389fef29b0cd99c6ebf921f7fea235e84a05 Mon Sep 17 00:00:00 2001 From: Nehal Patel Date: Fri, 7 Aug 2015 20:05:00 -0500 Subject: [PATCH] Added the IFTTTHandler --- src/Monolog/Handler/IFTTTHandler.php | 64 ++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/Monolog/Handler/IFTTTHandler.php diff --git a/src/Monolog/Handler/IFTTTHandler.php b/src/Monolog/Handler/IFTTTHandler.php new file mode 100644 index 00000000..7dcaebd5 --- /dev/null +++ b/src/Monolog/Handler/IFTTTHandler.php @@ -0,0 +1,64 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Monolog\Handler; + +use Monolog\Logger; + +/** + * IFTTTHandler uses cURL to trigger IFTTT Maker actions + * + * @author Nehal Patel + */ +class IFTTTHandler extends AbstractProcessingHandler +{ + protected $_eventName; + protected $_secretKey; + + /** + * @param string $eventName The name of the IFTTT Maker event that should be triggered + * @param string $secretKey A valid IFTTT secret key + * @param integer $level The minimum logging level at which this handler will be triggered + * @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not + */ + public function __construct($eventName, $secretKey, $level = Logger::ERROR, $bubble = true) + { + $this->_eventName = $eventName; + $this->_secretKey = $secretKey; + + parent::__construct($level, $bubble); + } + + /** + * {@inheritdoc} + */ + public function write(array $record) + { + $postData = array( + "value1" => $record["channel"], + "value2" => $record["level_name"], + "value3" => $record["message"] + ); + $postString = json_encode($postData); + + $ch = curl_init(); + + curl_setopt($ch, CURLOPT_URL, "https://maker.ifttt.com/trigger/" . $this->_eventName . "/with/key/" . $this->_secretKey); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, $postString); + curl_setopt($ch, CURLOPT_HTTPHEADER, array( + "Content-Type: application/json" + )); + + Curl\Util::execute($ch); + } +} \ No newline at end of file