php vcard display as plain text

孤人 提交于 2020-01-23 04:27:16

问题


i have a new feature on my site that allow user to get vcard files. this feature reads data from my database (which this one works) and generate the vcard.

the file is: vcard.php and i pass the user id as GET

then i get all the information using that id

my problem is when i want to get the vcard, it display as text. here's a simplified version of my code:

<?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();

the code works, why it does not get it as vcard?


回答1:


Given http://en.wikipedia.org/wiki/VCard

You certainly need to add the following line before echo $v->get();

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

in order to tell the client's browser that it will receive a VCard file.



来源:https://stackoverflow.com/questions/8688671/php-vcard-display-as-plain-text

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!