Sep 20

PHP and COM: Quite Awesome These Days

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.


Comments

by Chris,   September 22, 2005 5:52 PM  

Giving up VBScript forever? Are you able to code an Outlook form, such as a Post or Contact, with php?


by Deane,   September 22, 2005 10:39 PM  

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.


by Amit,   June 26, 2008 10:47 AM  

Hi

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



Add Comment


Want to advertise on this site? Contact FM.
Laser Toner Cartridges UK laser toner, toner cartridges, hp toner, lexmark toner, samsung toner, canon, toner, epson toner, oki toner, kyocera toner, xerox toner, remanufactured toner, compatible toner
Direct TV Deals Free 4 room direct tv deals. no equipment to buy. free fast professional direct tv installation. this is the best direct tv deal available anywhere.
SEO Article Learn from the experts with our SEO article.
rope light Shopping with birddog distributing, inc., gives you access to the lowest prices, the best customer service and the quickest delivery times possible.
Laptop AC Adapter We offer genuine factory direct replacement AC adapters.
Direct TV Best satellite TV deals.
Direct TV Deals Direct TV programming deals are varied and include packages containing from 50 channels up to over 250 channels.
8mm film to DVD Retain family memories with the only frame by frame digital restoration service in the United States for your 8mm film to DVD today
Rubber Stamp Shop for custom self-inking stamps, hand stamps, address stamps, label stamps, check endorsement stamps, check deposit stamps, date stamps, pre inks, pocket stamps, ink and much more!