我的网站上有一项新功能,允许用户获取vcard文件。此功能从我的数据库中读取数据(此功能有效)并生成vcard。
该文件是:vcard.php,我将用户ID传递为GET
然后我使用该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获得呢?
最佳答案
给定http://en.wikipedia.org/wiki/VCard
您当然需要在echo $v->get();
之前添加以下行
header('Content-Type: text/vcard');
为了告诉客户的浏览器它将收到一个VCard文件。
关于php - php vcard显示为纯文本,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8688671/