Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php vcard display as plain text

Tags:

php

vcf-vcard

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?

like image 430
goblar Avatar asked Dec 31 '11 14:12

goblar


1 Answers

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.

like image 122
Frosty Z Avatar answered Sep 22 '22 12:09

Frosty Z