Subject RE: [firebird-php] PHP-CLI and event handling
Author Nigel Weeks
PHP and events is amazingly easy! Relax!
Here's a complete walkthrough (PHP5-cli, FreeBSD)
Note:
The backticks '`' indicate a command. Leave them out of the command you
type
My php interpreter is at '/usr/local/bin/php'. Yours might be different
I like beer, so it features in my scripts.


Create yourself a simple script, like so:

-------- BEGIN LISTING of 'listen.php' -------
#!/usr/local/bin/php -e
<?php

$conn = ibase_connect("server:/path/to/db.fdb","sysdba","masterkey");


function respond($event){
switch($event){
case "A000":
echo "Oh dear. Event ".$event." has occured. Time for beer\n";
break;

default:
echo "Event ".$event." occured, but no handler exists. Just letting
you know...\n";
break;
} // End of switch on event
} // End of function respond


// Set the 'respond' function to listen for these event codes
ibase_set_event_handler("respond","A000","A001","A002");

// Now, go to sleep, waiting for events
while(1){
sleep(1);
}

?>
---------- END LISTING ----------


Make the script executable with a `chmod +x listen.php`


Now, we create a tiny stored procedure to post events:

-------- START LISTING OF 'poster.sql' --------
SET TERM !! ;
CREATE PROCEDURE poster (
str_code VARCHAR(5)
) AS BEGIN
POST_EVENT str_code;
END !!
SET TERM ; !!
--------- END LISTING --------------

Now, slurp the stored procedure into your database:
`isql -u sysdba -p masterkey server:/path/to/db.fdb < poster.sql`

Right. Now for the fun part!
Launch the listener:
`./listen.php`

It'll sit there, not doing much yet.

In another window, open up isql, and fire off a few events:
`isql -u sysdba -p masterkey server:/path/to/db.fdb`
`execute procedure poster('A000');`

The PHP listener will echo out:
"Oh dear. Event A000 has occured. Time for beer"

Try sending other codes it's registered for, and you'll get:
"Event <code here> occured, but no handler exists. Just letting you know..."

If you send an event it's not registered for, nothin happens!

Easy!

Nige.