Touching server before executing a URL (Exception Handling)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ibanez270dx
Forum Commoner
Posts: 74
Joined: Thu Jul 27, 2006 12:06 pm
Location: Everywhere, California

Touching server before executing a URL (Exception Handling)

Post 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
nincha
Forum Contributor
Posts: 191
Joined: Fri Mar 28, 2003 12:30 pm
Location: CA, USA

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

Post by nincha »

Have you considered the CURL library?
ibanez270dx
Forum Commoner
Posts: 74
Joined: Thu Jul 27, 2006 12:06 pm
Location: Everywhere, California

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

Post 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!
ibanez270dx
Forum Commoner
Posts: 74
Joined: Thu Jul 27, 2006 12:06 pm
Location: Everywhere, California

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

Post by ibanez270dx »

perhaps I could check if my WEBrick service is running on port 4243? Is there a way to do that?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

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

Post 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.
ibanez270dx
Forum Commoner
Posts: 74
Joined: Thu Jul 27, 2006 12:06 pm
Location: Everywhere, California

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

Post 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...
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

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

Post by Kieran Huggins »

maybe http://ca.php.net/function.exec would be a better solution for this? No second web server needed.
Post Reply