How to include your twitter on any php-enabled website

We are to do this by creating two files you put serverside. The first one, can be called twitter.php. Here’s the code:

<a href=”http://www.twitter.com/pan100lion”>

<?php
$twitterDOM = new DOMDocument();
$twitterDOM->encoding = ‘UTF-8′;
$twitterDOM->load(”http://twitter.com/statuses/user_timeline/37750230.rss”);

$xslt= new DOMDocument();
$xslt->encoding = ‘UTF-8′;
$xslt->load(”twitterstub.xsl”);

$processor = new XSLTProcessor();
$processor->importStylesheet($xslt);
$output = $processor->transformToDoc($twitterDOM);
echo $output->saveXML();
?>
</a>

Swap the link on the line $twitterDOM->load with your own feed adress. You’ll find it at your twitter page.

File number 2 is called twitterstub.xsl:

<!DOCTYPE xsl:stylesheet  [
<!ENTITY nbsp   "&#160;">
<!ENTITY copy   "&#169;">
<!ENTITY reg    "&#174;">
<!ENTITY trade  "&#8482;">
<!ENTITY mdash  "&#8212;">
<!ENTITY ldquo  "&#8220;">
<!ENTITY rdquo  "&#8221;">
<!ENTITY pound  "&#163;">
<!ENTITY yen    "&#165;">
<!ENTITY euro   "&#8364;">
]>
<xsl:stylesheet xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”
xmlns=”http://www.w3.org/1999/xhtml”
version=”1.0″ >

<xsl:output method=”html”
media-type=”text/html”
doctype-public=”-//W3C//DTD XHTML 1.0 Transitional//EN”
doctype-system=”http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”
indent=”yes”
encoding=”UTF-8″ />
<xsl:template match=”/”>
<xsl:for-each select=”rss/channel/item”>
<xsl:if test=”position() &lt; 2″>
<p><xsl:if test=”position() = 1″>
<xsl:attribute name=”style”>font-weight:bold; font-size:10px</xsl:attribute>
</xsl:if>
<xsl:value-of select=”title” />
<i>
(<xsl:value-of select=”pubDate” />)
</i>
</p>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

this is a stylesheet with info on how to parse the file. It is programmed to only take the first one, but it can be altered to take several posts if you know some xsl. Read more about xslt here.

Oh yeah, and you need to include the twitter.php file where you want it to appear. Like this: <?php include(’twitter.php’) ?>

5 Responses to “How to include your twitter on any php-enabled website”

  1. I’m getting the following error:

    Parse error: syntax error, unexpected ‘.’ in /home/kamnet/public_html/twitter.php on line 5

    which is:
    $twitterDOM->encoding = .UTF-8.;

    Any idea why this might be?

  2. I figured it out – apparently Wordpress’ fancy quotes are not copying over into plain text and messing it all up. *heh*

  3. I have another error:
    Fatal error: Class ‘XSLTProcessor’ not found in /var/www/twitter/twitter.php on line 11

    pls help!

  4. If you are hosting your server yourself: See if the extension php_xsl is active. If not turn it on. If someone else is hosting tell them to turn this extension on.

  5. It’s working now! Thank you!!!

Leave a Reply

Security Code: