X2V with PHP 5

Here are some code snippets to use the gorgeous X2V microformats XSLT files from Brian Suda with PHP 5 and libxslt (PHP 5 includes the XSL extension by default).

hCard to vCard:

To parse a XML or XHTML document, it has to be valid. To tidy the XML file, you can use a service from the W3C:

$xml_string_tidy = file_get_contents("http://cgi.w3.org/cgi-bin/tidy?docAddr=".urlencode($uri));Code-Sprache: PHP (php)

Then you have to create the HTML DOM…

@$document = new DOMDocument();
@$document->loadHTML($xml_string_tidy);Code-Sprache: PHP (php)

and the XSLT DOM…

$stylesheet = new DOMDocument();
$stylesheet->load('hcard2vcard.xsl');Code-Sprache: PHP (php)

Create a new XSLT Processor, load the Stylesheet…

$processor = new XsltProcessor();
$processor->importStylesheet($stylesheet);Code-Sprache: PHP (php)

and run the transformation.

$result = $processor->transformToDoc($document);
$str = $result->saveXML();Code-Sprache: PHP (php)

Now $str contains the transformed code.
To send the vCard header with PHP, try

header("Content-Disposition: attachment; filename=contact.vcf");
header("Content-Type: text/x-vcard; charset=UTF-8 name=contact.vcf");
echo $str;Code-Sprache: PHP (php)

If you want to use the vCard extension .vcf instead of .php you have to add something like that to your .htaccess file

RewriteEngine On
RewriteBase /
RewriteRule ^contact.vcf /hcard2vcard.php [L,QSA]

I hope it works 🙂

Matthias Pfefferle
Matthias Pfefferle
@pfefferle@notiz.blog

Ich bin Webworker und arbeite als „Open Web Lead“ @ Automattic. Ich blogge, podcaste und schreibe eine Kolumne über das open, independent und federated social Web. Mehr über mich.

767 Beiträge
806 Folgende

4 Kommentare zu „X2V with PHP 5“

  1. Is there any way to make this work with PHP 4? The intranet I’m working on only has PHP 4… Grr!

  2. Yes, it also works with PHP 4, but its a bit more code. Brian Suda uses PHP 4 for his Online Service: http://suda.co.uk/projects/X2V/ perhaps you could write him a mail…

  3. Thank you for this post. It has been so helpful to me:) Through following your code, I have made X2V work on static pages on my own server.

    Q. By referring your own copy of Brian Suda’s X2V to a URI on your own server, will it look for an actual file with that name or will it ask the server to retrieve the document?

  4. Avatar von Simon Rönnqvist
    Simon Rönnqvist

    Hi!

    I actually just asked Brian for his PHP-code (before I found this), and he gave me this link: https://web.archive.org/web/20090628123359/http://hg.microformats.org:80/x2v?cmd=manifest;manifest=3187422518eba12ad0c36c48b098439fea03b0dd;path=/;style=gitweb

    So it’s available from an older revision in the repo.

    cheers, Simon

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

To respond on your own website, enter the URL of your response which should contain a link to this post’s permalink URL. Your response will then appear (possibly after moderation) on this page. Want to update or remove your response? Update or delete your post and re-enter your post’s URL again. (Find out more about Webmentions.)