422 422 - 22 days ago 6
PHP Question

vCard propagate with info from dB via php

Ok like a plonker I thought it would be easy as.

We have a popup with clients details in, to look like a vCard ( but heavily styled ) all propagated via our db on the fly.

So I just thought yeah no worries, we can use php within the vcard, and parse the relevant info .. so that wneh user clicks d/l link they get the correct vCard details..

Hmmm nope, and finding info about vCards etc is hard.

So here is a snippet of the vcard.vcf

BEGIN:VCARD
VERSION:2.1
N;LANGUAGE=en-in:Name;Type;Your
FN:Type Your Name
TEL;WORK;VOICE:+1 (800) 123 4567


Example TEL is rendered in our php like:
<?=$r['tel'];?>


So how do we get php within the vcard to make it useable.. any hints appreciated.

Answer

The following is an example of a VCard file containing information for one person:

vCard 2.1:

BEGIN:VCARD
VERSION:2.1
N:Gump;Forrest
FN:Forrest Gump
ORG:Bubba Gump Shrimp Co.
TITLE:Shrimp Man
TEL;WORK;VOICE:(111) 555-1212
TEL;HOME;VOICE:            (404) 555-1212      
ADR;WORK:;;100 Waters Edge;Baytown;LA;30314;United States of America
LABEL;WORK;ENCODING=QUOTED-PRINTABLE:100 Waters Edge=0D=0ABaytown, LA 30314=0D=0AUnited States of   America
ADR;HOME:;;42 Plantation St.;Baytown;LA;30314;United States of America
LABEL;HOME;ENCODING=QUOTED-PRINTABLE:42 Plantation St.=0D=0ABaytown, LA 30314=0D=0AUnited States of America
EMAIL;PREF;INTERNET:forrestgump@example.com
REV:20080424T195243Z
END:VCARD

vCard 3.0:

BEGIN:VCARD
VERSION:3.0
N:Gump;Forrest
FN:Forrest Gump
ORG:Bubba Gump Shrimp Co.
TITLE:Shrimp Man
PHOTO;VALUE=URL;TYPE=GIF:http://www.example.com/dir_photos/my_photo.gif
TEL;TYPE=WORK,VOICE:(111) 555-1212
TEL;TYPE=HOME,VOICE:            (404) 555-1212      
ADR;TYPE=WORK:;;100 Waters Edge;Baytown;LA;30314;United States of America
LABEL;TYPE=WORK:100 Waters Edge\nBaytown, LA 30314\nUnited States of America
ADR;TYPE=HOME:;;42 Plantation St.;Baytown;LA;30314;United States of America
LABEL;TYPE=HOME:42 Plantation St.\nBaytown, LA 30314\nUnited States of America
EMAIL;TYPE=PREF,INTERNET:forrestgump@example.com
REV:20080424T195243Z
END:VCARD

Once you have all your data in a formant you can use say a string variable :

<?php
    header("Content-type: application/octet-stream");
    header("Content-Disposition: attachment; filename=\"vCard.vcf\"");
    echo $data;
?>

You can also if you wish create the file and off a link to that:

$ourFileName = "vCard.vcf";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);

I hope this puts you on track and that I have understood your question correctly.

ANSWER:

<?php
    $vCard = "BEGIN:VCARD
    VERSION:3.0
    N:Gump;Forrest
    FN:Forrest Gump
    ORG:Bubba Gump Shrimp Co.
    TITLE:Shrimp Man
    PHOTO;VALUE=URL;TYPE=GIF:http://www.example.com/dir_photos/my_photo.gif
    TEL;TYPE=WORK,VOICE:(111) 555-1212
    TEL;TYPE=HOME,VOICE:(404) 555-1212
    ADR;TYPE=WORK:;;100 Waters Edge;Baytown;LA;30314;United States of America
    LABEL;TYPE=WORK:100 Waters Edge\nBaytown, LA 30314\nUnited States of America
    ADR;TYPE=HOME:;;42 Plantation St.;Baytown;LA;30314;United States of America
    LABEL;TYPE=HOME:42 Plantation St.\nBaytown, LA 30314\nUnited States of America
    EMAIL;TYPE=PREF,INTERNET:forrestgump@example.com
    REV:20080424T195243Z
    END:VCARD";

    header("Content-type: application/octet-stream");
    header("Content-Disposition: attachment; filename=\"vCard.vcf\"");
    echo $vCard;
?>

The above code will allow the user to to download the vCard held in $vCard. In your case you would need to add your own data an example of how this would go is shown below:

    $vCard = "BEGIN:VCARD
    VERSION:3.0
    N:Gump;Forrest
    FN:Forrest Gump
    ORG:Bubba Gump Shrimp Co.
    TITLE:".$array['title']."
    PHOTO;VALUE=URL;TYPE=GIF:".$array['weblink']."
    TEL;TYPE=WORK,VOICE:".$array['WORKNUM']."
    TEL;TYPE=HOME,VOICE:".$array['HOMENUM']."
    ADR;TYPE=WORK:;;100 Waters Edge;Baytown;LA;30314;".$array['Country']."
    LABEL;TYPE=WORK:100 Waters Edge\nBaytown, LA 30314\n".$array['Country']."
    ADR;TYPE=HOME:;;42 Plantation St.;Baytown;LA;30314;".$array['Country']."
    LABEL;TYPE=HOME:42 Plantation St.\nBaytown, LA 30314\nUnited States of America
    EMAIL;TYPE=PREF,INTERNET:forrestgump@example.com
    REV:20080424T195243Z
    END:VCARD";