midrange.com code scratchpad
Name:
Check HTTP Logic
Scriptlanguage:
PHP
Tabwidth:
4
Date:
06/03/2010 09:11:54 pm
IP:
Logged
Description:
Here is the logic. I have a LogWriter class to dump values to a long that you'll see reference here. Obviously, I am using Zend Framework to perform the HTTP request, so I suppose the problem could lie in there, but we didn't update Zend Framework. The problem just started. Only other thing missing is some constants that define the text for certain errors...but that should be pretty obvious. This was created because I have a pretty firm grasp on how the IBM and Apache2 servers relate to each other in Zend Core but others here didn't. This command runs daily and can be run manually to diagnose and issue and make a suggestion to our system administrator on how to resolve it.
Code:
  1. <?php
  2.  
  3. require_once('Zend/Loader.php');
  4.  
  5. Zend_Loader::loadClass('Zend_Mail');
  6. Zend_Loader::loadClass('Zend_Mail_Transport_Smtp');
  7. Zend_Loader::loadClass('Zend_Http_Client');
  8. Zend_Loader::loadClass('Zend_Http_Client_Adapter_Exception');
  9.  
  10. /* OLD CODE
  11. $client = new Zend_Http_Client('https://www.data-management.com/php/status.php', array(
  12.     'maxredirects' => 0,
  13.     'timeout' => 30));
  14. */
  15.  
  16. // New Code - Can't check for SSL version of the site anymore for some
  17. $client = new Zend_Http_Client('http://www.data-management.com/php/status.php'array(
  18.     'maxredirects' => 0,
  19.     'timeout' => 30));
  20.  
  21. $client->setParameterGet('request''PROGRAM');
  22.  
  23. $responseBody = '';
  24. $error = '';
  25. $action = '';
  26.  
  27. try {
  28.     $response = $client->request();
  29.     //LogWriter::WriteVariable($response, 'checkhttp $response');
  30.     if ($response->isSuccessful()) {
  31.         $responseBody = $response->getBody();  
  32.         if ($responseBody == 'N') {
  33.             $error = "Problem with PHP sever.";
  34.             $action = ACTION_RESTARTZEND;
  35.         }
  36.     }
  37.     else {
  38.         switch ($response->getStatus()) {
  39.             case 502 :
  40.                 $error = "Bad gateway. PHP Server not started.";
  41.                 $action = ACTION_STARTPHP;
  42.                 break;
  43.             default : 
  44.                 $error = $response->getStatus() . ': ' . $response->getMessage();
  45.                 $action = ACTION_RESTARTZEND;
  46.                 break;            
  47.         }
  48.     }
  49. }
  50. catch(Zend_Http_Client_Adapter_Exception $err) {
  51.     //LogWriter::WriteVariable($err, 'checkhttp $err');
  52.     $response = null;
  53.     switch ($err->getCode()) {
  54.         case 0 :
  55.             $error = $err->getMessage();
  56.             $action = ACTION_STARTIBM;
  57.             break;
  58.         case 1000 :
  59.             $error = "Request timed out.";
  60.             $action = ACTION_RESTARTALL;
  61.             break;
  62.         default :
  63.             $error = $err->getCode() . ': ' . $err->getMessage();
  64.             $action = ACTION_RESTARTALL;
  65.             break;
  66.     }
  67. }
  68.  
  69.  
  70. //...code continues and email is sent.
© 2004-2019 by midrange.com generated in 0.012s valid xhtml & css