首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >php电子名片显示为纯文本

php电子名片显示为纯文本
EN

Stack Overflow用户
提问于 2011-12-31 22:36:03
回答 1查看 1.4K关注 0票数 15

我在我的网站上有一个新的功能,允许用户获得vcard文件。此功能从我的数据库(此功能有效)中读取数据并生成vcard。

这个文件是: vcard.php,我将用户id作为GET进行传递

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

我的问题是,当我想要得到vcard时,它显示为文本。以下是我的代码的简化版本:

代码语言:javascript
复制
<?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获得呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-12-31 22:38:45

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

当然,您需要在echo $v->get();之前添加以下行

代码语言:javascript
复制
header('Content-Type: text/vcard');

以便告诉客户端浏览器它将接收VCard文件。

票数 18
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8688671

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档