Page 1 of 1

Touching server before executing a URL (Exception Handling)

Posted: Fri Jun 06, 2008 5:56 pm
by ibanez270dx
Hello,
I have a PHP page that basically just takes some user input (dates) and puts them into a URL which points to a WEBrick server and initializes some Ruby scripts. Everything works perfectly fine, with one exception. I want to ensure that when my WEBrick server gets kicked or is down for some reason, that PHP can realize it and act accordingly. I tried some basic PHP exception handling, but to no avail. What I have is this:

Code: Select all

 
      try
        {
         echo "<META http-equiv=\"refresh\" content=\"0;URL=http://harrier:4243/cpa?from=$_POST[FromDate]&to=$_POST[ToDate]\">";
         exit;
        }
        catch(Exception $e)
        {
         echo 'Message: ' .$e->getMessage();
        }
 
...which leaves me with a 404 if my WEBrick server is down. Thus, I suppose I need PHP to "touch" and ensure the WEBrick server is alive before executing the META refresh tag. I got a suggestion to try file_get_contents like so:

Code: Select all

 
$str = file_get_contents( 'http://harrier:4243/cpa?from=2008-06-01&to=2008-06-10' );
 
if (  strpos( $str, '404' ) === FALSE )
  {
    echo 'The Server is Down';
  }
   else
  {
   echo 'The Server is Up';
  }
 
...but that doesn't work either and just gives me a 500 error. Can this be done? If so, how?

Any and all help is appreciated!

Thanks,
- Jeff

Re: Touching server before executing a URL (Exception Handling)

Posted: Fri Jun 06, 2008 6:06 pm
by nincha
Have you considered the CURL library?

Re: Touching server before executing a URL (Exception Handling)

Posted: Fri Jun 06, 2008 6:13 pm
by ibanez270dx
I have never used CURL... I will do some research on this. Also, a coworker of mine suggested using XML HTTP Requests, which I am also looking into... Would you happen to have an example of its implementation in regards to what I'm looking for?

Thanks!

Re: Touching server before executing a URL (Exception Handling)

Posted: Fri Jun 06, 2008 6:32 pm
by ibanez270dx
perhaps I could check if my WEBrick service is running on port 4243? Is there a way to do that?

Re: Touching server before executing a URL (Exception Handling)

Posted: Fri Jun 06, 2008 6:48 pm
by Kieran Huggins
Are you running webrick behind apache? How are you getting an HTTP error code without the server running?

Personally, I'd use Mongrel since it falls over a lot less and is MUCH faster.

Re: Touching server before executing a URL (Exception Handling)

Posted: Fri Jun 06, 2008 6:57 pm
by ibanez270dx
I'm running IIS6 with PHP 5 then using PHP to construct a URL which redirects to port 4243 where my WEBrick server is running. The variables defined in the URL are interpreted by WEBrick and sent to the appropriate Ruby script. It's kind of a workaround so that I don't have to run a Rails environment to use my Ruby scripts in a browser. I'm using PHP very briefly in this program. I just want to ensure that WEBrick is running from my PHP page before trying to access that URL. When the WEBrick server is running, it shows up on the task manager - just Ruby.exe. I was thinking perhaps PHP can "look" to see if Ruby.exe is running...

Re: Touching server before executing a URL (Exception Handling)

Posted: Fri Jun 06, 2008 7:49 pm
by Kieran Huggins
maybe http://ca.php.net/function.exec would be a better solution for this? No second web server needed.