Use vCards In Your PHP Code

Recently I found myself in a dilemma. I wanted an application to add a set of contacts to a list in a simple and easy way. Of course the new standard for exporting contacts creates a pretty little (.vcf) vCard file. In turn, I find myself writing my PHP code with the ability to read in vCard files. Here is a bare bones example of how you can do the same.

This here is a simple index.php

// index.php
<html>
<body>
<form enctype="multipart/form-data" action="reader.php" method="post">
VCF file: <input type="hidden" name="MAX_FILE_SIZE" value="2000000" />
<input type="file" name="file" />
<br/>
<input type="submit" value="Upload" />
</form>
</body>
</html>

And here is a corresponding reader.php

// reader.php
<html>
<body>
<?php
require_once('Contact_Vcard_Parse.php');
if ( $_FILES['file']['tmp_name'] )
{
$parse = new Contact_Vcard_Parse();
$cardinfo = $parse->fromFile($_FILES['file']['tmp_name']);
foreach( $cardinfo as $card )
{
$first = $card['N'][0]['value'][0][0];
$last = $card['N'][0]['value'][1][0];
$email = $card['EMAIL'][0]['value'][0][0];
?>
<a href="mailto:<?php echo($email); ?>">
<?php echo($first); ?> <?php echo($last); ?>
</a>
<br/>
<?php
}
}
?>
</body>
</html>

After reading in the temporary file, reader.php uses the vCard parsing object to place all the information from the vCard onto the page using PHP. This isn’t a very difficult project, but it can be quite useful to coder finding him/herself in a contact transfer dilemma. Why not stick to the standards? Use vCards in your PHP code to create an up to date webmail application so you can stop thinking about compatibility and focus on function, look and feel.

Posted on by Adam DeMoor in Computer Science, PHP
Tagged as: ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>