Hack65.Create vCard Files from Your Application s Data


Hack 65. Create vCard Files from Your Application's Data

Use a simple vCard template to create vCards dynamically from your application's data.

If you have a database of contacts on your web site, it's handy to put up a link to the email address of each contact, but wouldn't it be great to get all the information in one handy format? Well, it turns out that the vCard contact formula (VCF) is perfect for this task. And, you can create vCards automatically with PHP (as well as by reading the VCF format [Hack #64]).

6.16.1. The Code

Save the code in Example 6-44 as vcard.php.

Example 6-44. A vCard writer
 <?php  header( "Content-type:text/x-vCard" ); $first = "Howard";  $last = "Dean";  $email = "dean@dnc.org";  ?> BEGIN:VCARD VERSION:2.1 N:<?php echo($last); ?>;<?php echo($first); ?> FN:<?php echo($first); ?> <?php echo($last); ?> EMAIL;PREF;INTERNET:<?php echo($email); ?> REV:20050626T024452Z END:VCARD 

6.16.2. Running the Hack

Upload the code to your server and surf to it in your browser. You should get a download box similar to Figure 6-36.

Figure 6-36. The download dialog that opens after surfing to vcard.php


Accept the default action, which turns out (on Windows) to very conveniently launch Outlook and create a new contact file. You can add this person to your contact list by clicking on the "Save and Close" option, as shown in Figure 6-37.

The secret sauce is really the Content-type header item, which tells the downloading client that this is a vCard and not just plain text. Once that's in place, the operating system takes over and sends the vCard to the user's application that is set up to handle vCards.

In the unlikely event that your machine has no client that can read vCards, you will just get the option to save the file somewhere on your machine.


Figure 6-37. Importing the vCard into Outlook auto-magically


6.16.3. See Also

  • "Import Information from vCards" [Hack #64]



PHP Hacks
PHP Hacks: Tips & Tools For Creating Dynamic Websites
ISBN: 0596101392
EAN: 2147483647
Year: 2006
Pages: 163

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net