Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Created by PhpStorm.
- * User: bathan
- * Date: 4/11/16
- * Time: 3:32 PM
- *
- * FTP
- * v1f9wS)4^fGa9)g
- * marketrealist@totisoft.com.ar
- *
- */
- class MR_Feed_Monitor
- {
- private $_notify_email_address = 'miguel@marketrealist.com';
- private $_send_email = false;
- /**
- * Feeds class instance property
- *
- * @var array
- */
- var $feeds = array();
- /**
- * Default feeds
- *
- * @var array
- */
- private $_default_feeds = [
- 'yahoo-finance' => [
- 'url'=>'http://marketrealist.com/feed-yahoo-finance/', //-- Very Important to have slashes both at beggining and end
- 'username'=>'yahoofinancefeed',
- 'password'=>'ED7XaJBiy4HKPMKQqKFPWfW'
- ]
- ];
- /**
- * @param array $feeds
- */
- public function __construct( $feeds = array() )
- {
- if(empty($feeds)) {
- $feeds = $this->_default_feeds;
- }
- $this->feeds = $feeds;
- }
- public function set_notify_email($e) {
- $this->_notify_email_address = $e;
- }
- public function set_send_emails($s) {
- $this->_send_email = $s;
- }
- /**
- * Set class instance feeds
- *
- * @param $feeds
- */
- public function set_feeds( $feeds )
- {
- $this->feeds = $feeds;
- }
- /**
- *
- * Runs the Checks for the feeds
- */
- public function run_check()
- {
- foreach ($this->feeds as $feed_name=>$feed) {
- $feed_url = $feed['url'];
- $feed_username = $feed['username'];
- $feed_password = $feed['password'];
- $feed_contents = $this->get_feed_contents($feed_url,$feed_username,$feed_password);
- libxml_use_internal_errors(true);
- $doc = simplexml_load_string($feed_contents);
- $error_array = [];
- if (!$doc) {
- $errors = libxml_get_errors();
- foreach ($errors as $error) {
- $error_array[] = json_encode($error,JSON_PRETTY_PRINT);
- }
- libxml_clear_errors();
- //-- Ok Something is wrong, we need to send an email notification.
- if($this->_send_email) {
- $this->send_alert_email($this->_notify_email_address,$feed_name,$feed_url, $error_array);
- }
- }
- }
- }
- public function get_feed_contents($feedUrl,$feed_username,$feed_password)
- {
- $feed_content = null;
- try {
- $process = curl_init($feedUrl);
- curl_setopt($process, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
- //curl_setopt($process, CURLOPT_HEADER, 1);
- curl_setopt($process, CURLOPT_USERPWD, $feed_username . ":" . $feed_password);
- curl_setopt($process, CURLOPT_TIMEOUT, 300);
- curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);
- $feed_content = curl_exec($process);
- } catch (Exception $e) {
- throw $e;
- }
- return $feed_content;
- }
- private function send_alert_email($email,$feed_name,$feed_url,$errors) {
- $subject = $feed_name.' feed in trouble';
- $body = "<h2> Problem with ".$feed_name."</h2><h3>$feed_url</h3><pre>".print_r($errors,true)."</pre>";
- $headers = "From: MR Feeds Monitor <mrfm@marketrealist.com>\r\n";
- $headers .= "X-Mailer: TotiEmailer\n";
- $headers .= 'MIME-Version: 1.0' . "\n";
- $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
- $email_sent = mail($email,$subject, $body, $headers);
- echo $body ;
- return $email_sent;
- }
- }
- $send_emails = isset($_GET['send_email']);
- $x = new MR_Feed_Monitor();
- $x->set_send_emails($send_emails);
- if(isset($_GET['notify_email_address'])) {
- $x->set_notify_email($_GET['notify_email_address']);
- }
- $get_parameters = ['feed_name','feed_url','feed_username','feed_password'];
- $use_get_params = true;
- foreach($get_parameters as $param) {
- if(!isset($_GET[$param])) {
- $use_get_params = false;
- }
- }
- if($use_get_params) {
- $feed_name = $_GET['feed_name'];
- $feed_url = $_GET['feed_url'];
- $feed_username = $_GET['feed_username'];
- $feed_password = $_GET['feed_password'];
- $default_feeds = [
- $feed_name => [
- 'url'=>$feed_url, //-- Very Important to have slashes both at beggining and end
- 'username'=>$feed_username,
- 'password'=>$feed_password
- ]
- ];
- $x->set_feeds($default_feeds);
- }
- $x->run_check();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement