Open dynamically created Vcard(php)with ios/iPhone

h7appiyu  于 2023-10-15  发布在  PHP
关注(0)|答案(1)|浏览(99)

我改编了一个脚本来动态填充/创建和通过php下载vcard。它在android上运行良好,但在iPhone/ios上就不行了。我试了几个版本。我可以下载一个文件,但是打不开。我正在从一个xml文件中加载数据,供参考。
PS:我忘了补充,当我发送桌面电脑下载vcard并发送到iPhone,我可以打开它.但是直接下载的话打不开。

数据编码

这部分只是展示了我是如何解析xml数据的。我认为解决这个问题并不重要,但我还是添加了它。

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;

IMG转换器

这个脚本将img格式化为Base64格式,这样就很容易用于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;
}

版本1:

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>';

版本2:

$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>';
ghhaqwfi

ghhaqwfi1#

在主页上,我已经删除了大部分信息。现在有一个链接到一个单独的vcard.php创建vcard。

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

我创建了一个单独的文件vcard.php,其中包括以下代码:
vcard再次重新加载xml和它的变量。

xml解析

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;

相关问题