【问题标题】:php vcard display as plain textphp vcard显示为纯文本
【发布时间】:2012-01-31 02:23:50
【问题描述】:

我的网站上有一项新功能,允许用户获取 vcard 文件。此功能从我的数据库中读取数据(这个可以工作)并生成 vcard。

文件是:vcard.php,我将用户 ID 作为 GET 传递

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

我的问题是当我想获取电子名片时,它显示为文本。这是我的代码的简化版本:

<?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 获取?

【问题讨论】:

    标签: php vcf-vcard


    【解决方案1】:

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

    你当然需要在echo $v-&gt;get();前面加上下面这行

    header('Content-Type: text/vcard');
    

    为了告诉客户端的浏览器它将收到一个 VCard 文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-08
      • 2016-07-25
      • 2015-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-18
      • 2013-05-19
      相关资源
      最近更新 更多