如何使用XSLT转换轻松显示您的推文

Twitter tutorial. Today I will tell you how to create application which will display your Tweets (or someone else) at your website. And we will use XSLT transformation to parse XML (RSS) provided by Twitter. As example I take my Twitter account. At this page you can see that I have RSS channel: http://twitter.com/statuses/user_timeline/83882961.rss. Today I`ll show you how to display this information in any view.

Twitter教程。 今天,我将告诉您如何创建在您的网站上显示您的Tweets(或其他人)的应用程序。 我们将使用XSLT转换来解析Twitter提供的XML(RSS)。 以我的Twitter帐户为例。 在此页面上,您可以看到我有RSS频道:http://twitter.com/statuses/user_timeline/83882961.rss。 今天,我将向您展示如何在任何视图中显示此信息。

步骤1. PHP (Step 1. PHP)

Ok, here are all used PHP files:

好的,这是所有使用过PHP文件:

index.php (index.php)

This file will generate list of Twitter posts. All code commented, so I hope it will easy to understand it.

该文件将生成Twitter帖子列表。 所有代码均已注释,因此希望它易于理解。

<?php
$sMethod = 'http://twitter.com/statuses/user_timeline/83882961.rss';
$sXmlSrc = getTwitterStoriesXml($sMethod);
// Load the XML source
$xml = new DOMDocument;
$xml->loadXML($sXmlSrc);
// Load XSLT
$xsl = new DOMDocument;
$xsl->load('xslt/twitter.xslt');
$proc = new XSLTProcessor; // configure the transformer
$proc->importStyleSheet($xsl); // attach the xsl rules
echo '<link media="all" href="css/styles.css" type="text/css" rel="stylesheet">';
echo $proc->transformToXML($xml);
// this function get twitter information using caching (not more 1 times per minute)
function getTwitterStoriesXml($sUrl) {
    // our folder with cache files
    $sCacheFolder = 'cache/';
    // cache filename
    $sFilename = date('YmdHi').'.xml';
    if (! file_exists($sCacheFolder.$sFilename)) {
        $ch = curl_init($sUrl);
        $fp = fopen($sCacheFolder.$sFilename, 'w');
        curl_setopt($ch, CURLOPT_FILE, $fp);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_HTTPHEADER, Array('User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15') ); // makes our request look like it was made by Firefox
        curl_exec($ch);
        curl_close($ch);
        fclose($fp);
    }
    return file_get_contents($sCacheFolder.$sFilename);
}
?>
<?php
$sMethod = 'http://twitter.com/statuses/user_timeline/83882961.rss';
$sXmlSrc = getTwitterStoriesXml($sMethod);
// Load the XML source
$xml = new DOMDocument;
$xml->loadXML($sXmlSrc);
// Load XSLT
$xsl = new DOMDocument;
$xsl->load('xslt/twitter.xslt');
$proc = new XSLTProcessor; // configure the transformer
$proc->importStyleSheet($xsl); // attach the xsl rules
echo '<link media="all" href="css/styles.css" type="text/css" rel="stylesheet">';
echo $proc->transformToXML($xml);
// this function get twitter information using caching (not more 1 times per minute)
function getTwitterStoriesXml($sUrl) {
    // our folder with cache files
    $sCacheFolder = 'cache/';
    // cache filename
    $sFilename = date('YmdHi').'.xml';
    if (! file_exists($sCacheFolder.$sFilename)) {
        $ch = curl_init($sUrl);
        $fp = fopen($sCacheFolder.$sFilename, 'w');
        curl_setopt($ch, CURLOPT_FILE, $fp);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_HTTPHEADER, Array('User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15') ); // makes our request look like it was made by Firefox
        curl_exec($ch);
        curl_close($ch);
        fclose($fp);
    }
    return file_get_contents($sCacheFolder.$sFilename);
}
?>

步骤2. CSS (Step 2. CSS)

Here are used CSS file. Just few styles for our demo:

这是用过CSS文件。 我们的演示仅提供几种样式:

css / styles.css (css/styles.css)
h1{text-align:center}
.tw-unit{background-color:#F3F3F3;border:1px solid #CCC;position:relative;margin:10px;padding:10px}
body{background:#eee;font-family:Verdana, Helvetica, Arial, sans-serif;margin:0;padding:0}
.main{background:#FFF;width:900px;font-size:80%;border:1px #000 solid;margin:3.5em auto 2em;padding:1em 2em 2em}
h1{text-align:center}
.tw-unit{background-color:#F3F3F3;border:1px solid #CCC;position:relative;margin:10px;padding:10px}
body{background:#eee;font-family:Verdana, Helvetica, Arial, sans-serif;margin:0;padding:0}
.main{background:#FFF;width:900px;font-size:80%;border:1px #000 solid;margin:3.5em auto 2em;padding:1em 2em 2em}

步骤3. XSLT (Step 3. XSLT)

And, most interesting part of my story – used XSLT rules:

而且,我故事中最有趣的部分–使用了XSLT规则:

xslt / twitter.xslt (xslt/twitter.xslt)
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="channel">
        <xsl:variable name="channelDescription" select="description"/>
        <xsl:variable name="authorLink" select="link"/>
        <xsl:variable name="authorName" select="substring(title, 10)"/> <!-- we will cut off Twitter / -->
        <xsl:variable name="authorNameLength" select="string-length($authorName)+2"/> <!-- we will calculate length of our name + 2 symbols more (for colon and space symbols) -->
        <html>
            <body>
                <div class="main">
                <h1><xsl:value-of select="$channelDescription"/></h1>
                <xsl:for-each select="item">
                    <div class="tw-unit">
                        <h3>
                            <a target="_blank">
                                <xsl:attribute name="title"><xsl:value-of select="substring(title, $authorNameLength)"/></xsl:attribute>
                                <xsl:attribute name="href"><xsl:value-of select="link"/></xsl:attribute>
                                <xsl:value-of select="substring(title, $authorNameLength)"/>
                            </a>
                        </h3>
                        <div>Submitted by:
                            <a>
                            <xsl:attribute name="href">
                                <xsl:value-of select="$authorLink"/>
                            </xsl:attribute>
                            <xsl:value-of select="$authorName"/>
                            </a>
                        </div>
                        <div>Description: <xsl:value-of select="substring(description, $authorNameLength)"/></div>
                        <div>Source: <xsl:value-of select="*[name()='twitter:source']"/></div>
                        <div>When: <xsl:value-of select="substring(pubDate, 0, 26)"/></div> <!-- we will take first 26 symbols -->
                    </div>
                </xsl:for-each>
                </div>
                <xsl:comment>Copyright © AndrewP</xsl:comment>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="channel">
        <xsl:variable name="channelDescription" select="description"/>
        <xsl:variable name="authorLink" select="link"/>
        <xsl:variable name="authorName" select="substring(title, 10)"/> <!-- we will cut off Twitter / -->
        <xsl:variable name="authorNameLength" select="string-length($authorName)+2"/> <!-- we will calculate length of our name + 2 symbols more (for colon and space symbols) -->
        <html>
            <body>
                <div class="main">
                <h1><xsl:value-of select="$channelDescription"/></h1>
                <xsl:for-each select="item">
                    <div class="tw-unit">
                        <h3>
                            <a target="_blank">
                                <xsl:attribute name="title"><xsl:value-of select="substring(title, $authorNameLength)"/></xsl:attribute>
                                <xsl:attribute name="href"><xsl:value-of select="link"/></xsl:attribute>
                                <xsl:value-of select="substring(title, $authorNameLength)"/>
                            </a>
                        </h3>
                        <div>Submitted by:
                            <a>
                            <xsl:attribute name="href">
                                <xsl:value-of select="$authorLink"/>
                            </xsl:attribute>
                            <xsl:value-of select="$authorName"/>
                            </a>
                        </div>
                        <div>Description: <xsl:value-of select="substring(description, $authorNameLength)"/></div>
                        <div>Source: <xsl:value-of select="*[name()='twitter:source']"/></div>
                        <div>When: <xsl:value-of select="substring(pubDate, 0, 26)"/></div> <!-- we will take first 26 symbols -->
                    </div>
                </xsl:for-each>
                </div>
                <xsl:comment>Copyright © AndrewP</xsl:comment>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

第4步。重要注意事项 (Step 4. Few important notes)

During checking my PHP file you will notice that I perform caching of Twitter requests. I don`t load provided RSS often that once per minute. No need overload Twitter server with often requests :)

在检查我PHP文件期间,您会注意到我执行了Twitter请求的缓存。 我通常每分钟一次不提供RSS。 不需要经常请求的超载Twitter服务器:)

I made ‘cache’ folder with permission to write (you should create ‘cache’ folder too).

我取得了“ cache”文件夹的写权限(您也应该创建“ cache”文件夹)。

Also I created .htaccess file in it with next content:

我也用下面的内容在其中创建了.htaccess文件:

Options -Indexes

选项-索引

That’s all. After this, you will able to load not only my RSS feeds (of 83882961.rss), but also and your own too.

就这样。 之后,您不仅可以加载我的RSS feed(为83882961.rss),而且还可以加载您自己的RSS feed。

[sociallocker]

[社交储物柜]

打包下载

[/sociallocker]

[/ sociallocker]

结论 (Conclusion)

I hope that today’s article will very useful for your projects. Good luck!

我希望今天的文章对您的项目非常有用。 祝好运!

翻译自: https://www.script-tutorials.com/how-to-easily-display-twitter-posts-using-xslt-transformation/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值