test(); * * The url "http://www.quirkey.com" is requested and if the response code is 200 a success message * will be logged in the log file specified. If an error occurs, the script attempts to send an email using * the options provided in the class definition * * * @author Aaron Quint * @url http://www.quirkey.com/blog/ **/ require_once 'Mail.php'; require_once 'HTTP/Request.php'; class TestURL { // url to test var $test_urls; var $current_url; // expected response code var $response_code; //expected response headers var $response_headers; var $request; var $response; // Email info var $from_email = "ADMIN EMAIL ADDRESS";// The address error mail will come from var $to_email = "MY EMAIL ADDRESS"; // // Mail Parameters - for the PEAR Mail Class var $mail_backend = "smtp";//set to your host sender - smtp/sendmail/etc... var $mail_params = array('host' => 'SMTPHOST');//Set to your host parameters // Place to log the different requests var $log_file = 'test_url_log'; // how should the time format look (using strftime()) var $log_time_format = "[%D - %T] "; /** * TestURL() * constructor - sets up a test * * @return void **/ function TestURL($test_urls, $response_code = false, $response_headers = false) { $this->test_urls = $test_urls; $this->response_code = $response_code; $this->response_headers = $response_headers; } /** * test() * after a TestURL object is initialized this guy runs the test * and evaluates the responses * * @param print_log whether or not to print the output of the tests * @return void **/ function test($print_log = false) { // if were only checking one url - force it into an array // so we can test them all in the same way if (!is_array($this->test_urls)) { $test_url = $this->test_urls; $this->test_urls = array($test_url); } // go through the urls and test them foreach ($this->test_urls as $url) { $this->current_url = $url; $this->ping(); $this->validateResponse($print_log); } } /** * ping() * ping a url provided by $this->current_url and collect the response * makes good use of the HTTP_Request class * * @return void **/ function ping() { // set up request and send it $this->request =& new HTTP_Request($this->current_url); $this->response = $this->request->sendRequest(); } /** * vaildateResponse() * parses the response after a ping is sent. If an error occurs an email alert is sent. * all action is logged to a file * * @param print_log whether or not to print the output of the tests * @return boolean test Succeess? **/ function validateResponse($print_log) { if (!$this->response) { $message = "Unkown Error:" . $this->current_url; $this->sendErrorEmail($message); $this->logToFile($message); if ($print_log) { echo $message . "\n"; } return false; } // Check if response is a Pear Error if (PEAR::isError($this->response)) { $message = "PEAR Error:" . $this->current_url . ":" . $this->response->getMessage(); $this->sendErrorEmail($message); $this->logToFile($message); if ($print_log) { echo $message . "\n"; } return false; } //Check if response code doesnt match expected code if ($this->response_code && $this->request->getResponseCode() != $this->response_code) { $message = "Wrong Response Code - Expected {$this->response_code}, Got " . $this->request->getResponseCode() . ": " . $this->current_url; $this->sendErrorEmail($message); $this->logToFile($message); if ($print_log) { echo $message . "\n"; } return false; } // Check is expected headers match if ($this->response_headers) { foreach ($this->response_headers as $header => $val) { if (!($val == $this->request->getResponseHeader($header))) { //header doesnt match $message = "Headers dont match - Expected {$header}: {$val}, Got {$header}: " . $this->request->getResponseHeader($header) . ": " . $this->current_url; $this->sendErrorEmail($message); $this->logToFile($message); if ($print_log) { echo $message . "\n"; } return false; } } } // Tests passed // log success return true $message = "Success: " . $this->current_url; $this->logToFile($message); if ($print_log) { echo $message . "\n"; } return true; } /** * logToFile() * Logs a message to the file provided in the class def ($this->log_file) * * @return void **/ function logToFile($message) { // open the file for appending $fp = fopen($this->log_file,'a'); // prepend time to message $message = strftime($this->log_time_format,time()) . $message . "\n"; // write to file fwrite($fp,$message); //close the file fclose($fp); } /** * sendErrorEmail() * Sends a log message to the email address in the class def ($this->to_email) * also uses the params for the mail server set in the class def * * @return void **/ function sendErrorEmail($message) { $headers['To'] = $this->to_email; $headers['From'] = $this->from_email; $headers['Subject'] = "[TestURL] Error"; $body = strftime($this->log_time_format,time()) . $message . "\n"; $message =& Mail::factory($this->mail_backend,$this->mail_params); $res = $message->send($this->to_email, $headers, $body); return $res; } } ?>