Skip to content

Instantly share code, notes, and snippets.

@fordnox
Created December 3, 2012 13:07
Show Gist options
  • Save fordnox/4194934 to your computer and use it in GitHub Desktop.
Save fordnox/4194934 to your computer and use it in GitHub Desktop.
WebHook notifier class
<?php
class WebHookEvent
{
private $name = null;
private $params = array();
public function setName($event)
{
$this->name = $event;
}
public function getName()
{
return $this->name;
}
public function setParams(array $params)
{
$this->params = $params;
}
public function getParams()
{
return $this->params;
}
}
class WebHookNotifier
{
private $urls = array();
private $event = null;
public function setUrls(array $urls)
{
$this->urls = $urls;
}
public function setEvent(WebHookEvent $event)
{
$this->event = $event;
}
public function notify()
{
$event = $this->event;
if(!$event instanceof WebHookEvent) {
throw new Exception('Event must be set before notification is sent');
}
if(!$event->getName()) {
throw new Exception('Event name must be set before notification is sent');
}
if(empty($this->urls)) {
//throw new Exception('No url were defined');
return false;
}
$params = $event->getParams();
$params['event'] = $event->getName();
foreach($this->urls as $url) {
try {
$this->call_url($url, $params);
} catch(Exception $e) {
error_log($e->getMessage());
}
}
}
private function call_url($url, array $params)
{
$timeout = 10;
$ch = curl_init();
$userAgent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)';
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
if (ini_get('open_basedir') == '' && ini_get('safe_mode') == 'Off'){
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $this->_followlocation);
}
$data = curl_exec($ch);
if($data === false) {
throw new Exception(curl_error($ch), curl_errno($ch));
}
curl_close($ch);
return $data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment