{"id":546,"date":"2007-08-12T20:44:31","date_gmt":"2007-08-12T18:44:31","guid":{"rendered":"http:\/\/notizblog.org\/2007\/08\/12\/x2v-with-php-5\/"},"modified":"2020-09-03T14:10:15","modified_gmt":"2020-09-03T12:10:15","slug":"x2v-with-php-5","status":"publish","type":"post","link":"https:\/\/notiz.blog\/2007\/08\/12\/x2v-with-php-5\/","title":{"rendered":"X2V with PHP 5"},"content":{"rendered":"\n<p>Here are some code snippets to use the gorgeous <a href=\"https:\/\/suda.co.uk\/projects\/X2V\/\">X2V<\/a> microformats <abbr title=\"Extensible Stylesheet Language Transformation\">XSLT<\/abbr> files from <a href=\"https:\/\/suda.co.uk\/\">Brian Suda<\/a> with PHP 5 and <a href=\"https:\/\/www.php.net\/manual\/de\/book.xsl.php\">libxslt<\/a> (PHP 5 includes the XSL extension by default).<\/p>\n\n\n\n<p><strong>hCard to vCard<\/strong>:<\/p>\n\n\n\n<p>To parse a XML or XHTML document, it has to be valid. To tidy the XML file, you can use a service from the <a href=\"http:\/\/www.html-tidy.org\/\">W3C<\/a>:\n<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$xml_string_tidy = file_get_contents(<span class=\"hljs-string\">\"http:\/\/cgi.w3.org\/cgi-bin\/tidy?docAddr=\"<\/span>.urlencode($uri));<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code-Sprache:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Then you have to create the HTML DOM&#8230;\n<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">@$document = <span class=\"hljs-keyword\">new<\/span> DOMDocument();\n@$document-&gt;loadHTML($xml_string_tidy);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code-Sprache:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>and the XSLT DOM&#8230;\n<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$stylesheet = <span class=\"hljs-keyword\">new<\/span> DOMDocument();\n$stylesheet-&gt;load(<span class=\"hljs-string\">'hcard2vcard.xsl'<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code-Sprache:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Create a new XSLT Processor, load the Stylesheet&#8230;\n<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$processor = <span class=\"hljs-keyword\">new<\/span> XsltProcessor();\n$processor-&gt;importStylesheet($stylesheet);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code-Sprache:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>and run the transformation.\n<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">$result = $processor-&gt;transformToDoc($document);\n$str = $result-&gt;saveXML();<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code-Sprache:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Now <code>$str<\/code> contains the transformed code.<br\/> To send the vCard header with PHP, try<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">header(<span class=\"hljs-string\">\"Content-Disposition: attachment; filename=contact.vcf\"<\/span>);\nheader(<span class=\"hljs-string\">\"Content-Type: text\/x-vcard; charset=UTF-8 name=contact.vcf\"<\/span>);\n<span class=\"hljs-keyword\">echo<\/span> $str;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code-Sprache:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If you want to use the vCard extension .vcf instead of .php you have to add something like that to your <code>.htaccess<\/code> file<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\">RewriteEngine On\nRewriteBase \/\nRewriteRule ^contact.vcf \/hcard2vcard.php &#91;L,QSA]<\/code><\/span><\/pre>\n\n\n<p>I hope it works \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"webmentions_disabled_pings":false,"webmentions_disabled":false,"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":4,"activitypub_interaction_policy_quote":"anyone","activitypub_status":"","footnotes":""},"categories":[2],"tags":[166,164,381,380],"class_list":{"0":"post-546","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-web","7":"tag-hcard","8":"tag-microformats","9":"tag-vcard","10":"tag-x2v","11":"h-entry","12":"hentry"},"_links":{"self":[{"href":"https:\/\/notiz.blog\/wp-api\/wp\/v2\/posts\/546","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/notiz.blog\/wp-api\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/notiz.blog\/wp-api\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/notiz.blog\/wp-api\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/notiz.blog\/wp-api\/wp\/v2\/comments?post=546"}],"version-history":[{"count":2,"href":"https:\/\/notiz.blog\/wp-api\/wp\/v2\/posts\/546\/revisions"}],"predecessor-version":[{"id":20353,"href":"https:\/\/notiz.blog\/wp-api\/wp\/v2\/posts\/546\/revisions\/20353"}],"wp:attachment":[{"href":"https:\/\/notiz.blog\/wp-api\/wp\/v2\/media?parent=546"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/notiz.blog\/wp-api\/wp\/v2\/categories?post=546"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/notiz.blog\/wp-api\/wp\/v2\/tags?post=546"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}