PHP and COM: Quite Awesome These Days

By Deane Barker on September 20, 2005

I do some hairy Outlook and Exchange programming. Not by choice, but by force — I have users that won’t give up Outlook for any reason, and they want it to interoperate with our PHP-based database.

I usually do my Outlook scripting in VBScript, since Outlook has a snazzy COM API (COM = “Component Object Model” = Microsoft’s way to make other programs, like Outlook, do stuff from code). However, I recently put together a command line scripting environment for PHP (see this post), and I really didn’t want my scripts spread across two languages.

PHP started supporting COM back in version 4, but I had heard it was buggy and annoying, with PHP functions just to set values on COM objects and such (“com_get,” “com_set,” etc.). But, being a glutton for punishment, I gave it try anyway.

Oh my, how things have changed.

As of this writing, COM support in PHP is so good, I’d venture that you could take a VBScript, stick dollar signs in front of your variables, replace “.” with “->”, put some semi-colons in, and it would run. Getting a COM object is like this:

$outlookInstance = new COM("Outlook.Application");

From there, you can call methods and properties exactly as if it was a native PHP object.

$nameSpace = $outlookInstance->GetNamespace("MAPI");
$folder = $nameSpace->GetFolderFromID([...]);

The only problem I ran into was that COM didn’t like PHP nulls. Calling strval solved that problem.

I’m pretty smitten with this whole scenario. PHP is simply a more capable language than VBScript, and being able to access COM so easily means I can write better scripts faster in PHP. With this, I think I’ve jettisoned VBScript forever. Very cool.

Gadgetopia
What This Links To

Comments

  1. Are you able to code an Outlook form, such as a Post or Contact, with php?

    That’s not the kind of programming I do. I do batch processing, instantiating Outlook from code, or querying Exchange via the OLE DB driver.

    Yes, stuff like you describe would still have to be done with VBScript.

  2. Hi

    Thanks for this post this is great any ideas how to sync a web app with outlook calendar and events and vice versa

Comments are closed. If you have something you really want to say, email editors@gadgetopia.com and we‘ll get it added for you.