Приглашаем посетить
Кулинария (cook-lib.ru)

Hack 64. Import Information from vCards

Previous
Table of Contents
Next

Hack 64. Import Information from vCards

Hack 64. Import Information from vCards Hack 64. Import Information from vCards

Teach your PHP application to read the vCard standard for the storage of contact information.

All too often, we ask web application users to recode data in files that they already have, when a flexible application could simply read the user's (already-existing) files directly. An example of this is the vCard file, a universal mechanism for storing contact information, particularly information like email and physical addresses. Every reasonable email and address book program can import and export the vCard (.vcf ) file format.

This hack shows you how to read this format using the Contact_Vcard_ Parse PEAR module [Hack #2].

6.15.1. The Code

Save the code in Example 6-42 as index.php.

Example 6-42. A simple file upload page
<html> 
<body>
	<form enctype="multipart/form-data" action="read.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>

Save the code in Example 6-43 as read.php.

Example 6-43. The vCard reader
<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>

The real work in this hack is done in the read.php script; it reads the temporary downloaded file using the fromFile( ) method on the Contact_Vcard_ Parse object. The script then dumps each email it finds in the card using standard PHP text-templating techniques.

6.15.2. Running the Hack

Create a vCard file by exporting a contact from your email program or address book. Then upload the code to the server and surf to the index.php page. There you should see something similar to Figure 6-34.

Figure 6-34. The page that accepts vCard files for import
Hack 64. Import Information from vCards


Browse to the VCF file by clicking the Browse… button. Then click on the Upload button to send the file to the server. You should see something similar to Figure 6-35.

Figure 6-35. A simple import page that shows the name from the card with its email
Hack 64. Import Information from vCards


This page shows just the first and last name from the vCard, bracketed in an anchor tag that points to the person's email address. vCards can contain multiple records, and if the file contains many records, the user will see each in a list of individual links.

6.15.3. See Also

  • "Create vCard Files from Your Application's Data" [Hack #65]


Previous
Table of Contents
Next