phpiosiphonedynamicvcf-vcard

Open dynamically created Vcard (php) with ios/Iphone


I have adapted a script to dynamically fill/create and download a vcard via php. It works well with android, but not on the iphone/ios. I tried several versions. I can download a file, but I can't open it. I'm loading the data from an xml file, fyi.

PS: I forgot to add, that when I send the desktop computer downloaded vcard and send it to the iphone, I can open it. But downloading it directly, it won't open.

Data Code

This part just shows how I am parsing xml Data. I don't think it's important to solve the issue, but I added it anyway.

foreach ($contacts as $contact){
    if (isset($_GET['name']) && $_GET['name'] == $contact->name){
        $org        = "company";
        $tel        = "00000000";
        $street     = "Street;
        $city       = "City";
        $plz        = "00000000";
        $date       = "0000000";
        $www        = "www";
        $name       = $contact->name;
        $telefon    = $contact->tel;
        $title      = $contact->title;
        $fn         = $contact->fn;
        $ln         = $contact->ln;
        $mobil      = $contact->mobil;
        $email      = $contact->email;
        $img        = $contact->img;
        $imgfile    = "img/portraits/".$img.".jpg";
    }
}

Global Variables

Just making sure I can use the variables from the array outside of the array.

global $name; 
global $tel;
global $title;
global $fn;
global $ln;
global $email;
global $mobil;
global $imgfile;
global $company;
global $tel;
global $street;
global $city;
global $plz;
global $date;
global $www;

IMG Converter

This script formats the img into Base64 so it's easy to use for the vcard.

if($img!=""){ 
    $getPhoto    = file_get_contents($imgfile);
    $b64vcard    = base64_encode($getPhoto);
    $b64mline    = chunk_split($b64vcard,74,"\r\n");
    $b64final    = preg_replace('/(.+)/', ' $1', $b64mline);
    $photo       = $b64final;
}

Version1:

echo '<div class="vcard"><a href="data:text/plain;charset=UTF-8,BEGIN:VCARD
VERSION:3.0
N;CHARSET=utf-8:'.$ln.';'.$fn.';;;
FN;CHARSET=utf-8:'.$name.'
EMAIL;INTERNET:'.$email.'
ORG;CHARSET=utf-8:'.$org.'
TITLE;CHARSET=utf-8:'.$title.'
TEL;TEL;type=CELL:+49'.$mobil.'
TEL;TEL;type=Alternate Phone:+49'.$tel.'
ADR;TYPE=work:;;'.$street.';'.$city.' ;DE;'.$plz.' ;
URL;TYPE=website:'.$www.'
PHOTO;ENCODING=b;TYPE=JPEG:'.$photo.'
END:VCARD" download="'.$name.'1.vcf" class="btn">Vcard-Download</a></div>';

Version2:

$vcard = 'VERSION:3.0
N;CHARSET=utf-8:'.$ln.';'.$fn.';;;
FN;CHARSET=utf-8:'.$name.'
EMAIL:'.$email.'
ORG;CHARSET=utf-8:'.$org.'
TITLE;CHARSET=utf-8:'.$title.'
TEL;TEL;type=work:+49'.$tel.'
TEL;TEL;type=CELL:+49 '.$mobil.'
ADR;TYPE=work:;;'.$street.';'.$city.' ;DE;'.$plz.' ;
URL;TYPE=website:'.$www.'
PHOTO;ENCODING=b;TYPE=JPEG:'.$photo.'
END:VCARD'. "\r\n";

echo '<div class="vcard"><a href="data:text/plain;charset=UTF-8,BEGIN:VCARD
'.$vcard.'" download="'.$name.'2.vcf" class="btn">Test</a></div>';

Solution

  • On the main page, I have removed most of the info I had. There is now a link to a seperate vcard.php which creates the vcard.

    <a href="vcard.php?name=<?php echo $name ?>">Generate VCard</a>
    

    I have creared a seperate file vcard.php which includes the following code:

    The vcard reloads the xml again and it's variables again.

    xml parsing

    foreach ($contacts as $contact){
        if (isset($_GET['name']) && $_GET['name'] == $contact->name){
            $org        = "company";
            $tel        = "00000000";
            $street     = "Street;
            $city       = "City";
            $plz        = "00000000";
            $date       = "0000000";
            $www        = "www";
            $name       = $contact->name;
            $telefon    = $contact->tel;
            $title      = $contact->title;
            $fn         = $contact->fn;
            $ln         = $contact->ln;
            $mobil      = $contact->mobil;
            $email      = $contact->email;
            $img        = $contact->img;
            $imgfile    = "img/portraits/".$img.".jpg";
        }
    }
    
    global $name; 
    global $tel;
    global $title;
    global $fn;
    global $ln;
    global $email;
    global $mobil;
    global $imgfile;
    global $company;
    global $tel;
    global $street;
    global $city;
    global $plz;
    global $date;
    global $www;
    
      header('Content-Type: text/x-vcard');  
      header('Content-Disposition: inline; filename= "'.$name.'.vcf"');  
    
      if($img!=""){ 
        $getPhoto    = file_get_contents($imgfile);
        $b64vcard    = base64_encode($getPhoto);
        $b64mline    = chunk_split($b64vcard,74,"\r\n");
        $b64final    = preg_replace('/(.+)/', ' $1', $b64mline);
        $photo       = $b64final;
    }
    
      $vCard = "BEGIN:VCARD\r\n";
      $vCard .= "VERSION:3.0\r\n";
      $vCard .= "FN:" . $name . "\r\n";
      $vCard .= "TITLE:" . $org . "\r\n";
    
      if($email){
        $vCard .= "EMAIL;TYPE=internet,pref:" . $email . "\r\n";
      }
      if($getPhoto){
        $vCard .= "PHOTO;ENCODING=b;TYPE=JPEG:";
        $vCard .= $photo . "\r\n";
      }
    
      if($mobil){
        $vCard .= "TEL;TYPE=work,voice:" . $mobil . "\r\n"; 
      }
    
      $vCard .= "END:VCARD\r\n";
      echo $vCard;