PHP vCard显示为纯文本

15

我在我的网站上添加了一个新功能,允许用户获取vCard文件。该功能从我的数据库中读取数据(这个工作正常),然后生成vCard。

文件名为:vcard.php,并使用GET方式传递用户ID。

然后,我使用该ID获取所有信息。

我的问题是当我想获取vCard时,它会以文本形式显示。以下是我的代码的简化版本:

<?php

class Vcard {

  public function __construct() {
    $this->props = array();
  }

  public function setName($family, $first) {
    $name = $family . ';' . $first . ';';
    $this->props['N'] = $name;
    if(!isset($this->props['FN'])) {
      $display = $first . ' ';
      $display .= $family;
      $this->props['FN'] = trim($name);
    }
  }
  /* and all the rest of my props */
  public function get() {
    $text = 'BEGIN:VCARD' . "\r\n";
    $text.= 'VERSION:2.1' . "\r\n";
    foreach($this->props as $key => $value) {
      $text .= $key . ':' . $value . "\r\n";
    }
    $text.= 'REV:' . date("Y-m-d") . chr(9) . date("H:i:s") . "\r\n";
    $text.= 'MAILER:My VCard Generator' . "\r\n";
    $text.= 'END:VCARD' . "\r\n";
    return $text;
  }
}

$v = new Vcard();
$v->setName('Smith', 'John');
echo $v->get();

代码可以运行,为什么它不能作为vcard获取呢?

1个回答

18

给定http://en.wikipedia.org/wiki/VCard

echo $v->get();之前,您一定需要添加以下行:

header('Content-Type: text/vcard');
为了告诉客户端浏览器将会接收一个VCard文件。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接