将 tumblr 博客与网站集成

发布于 08-07 14:08 字数 177 浏览 7 评论 0原文

我想将我的 tumblr feed 集成到我的网站中。 tumblr 似乎有一个 API,但我不太清楚如何使用它。据我了解,我请求该页面,tumblr 返回一个包含我博客内容的 xml 文件。但是我怎样才能把这个 xml 变成有意义的 html 呢?我必须用php解析它,将相关标签变成标题等吗?我告诉自己这不会那么痛苦。有人有任何见解吗?

I would like to integrate my tumblr feed in to my website. It seems that tumblr has an API for this, but I'm not quite sure how to use it. From what I understand, I request the page, and tumblr returns an xml file with the contents of my blog. But how do I then make this xml into meaningful html? Must I parse it with php, turning the relevant tags into headers and so on? I tell myself it cannot be that painful. Anyone have any insights?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

飘然心甜2024-08-14 14:08:22

现在有一个 javascript include 可以执行此操作,可从 Tumblr 获取(您必须登录才能看到它): http://www .tumblr.com/developers

它最终是这样的:

<script type="text/javascript" src="http://{username}.tumblr.com/js"></script>

There's a javascript include that does this now, available from Tumblr (you have to login to see it): http://www.tumblr.com/developers

It winds up being something like this:

<script type="text/javascript" src="http://{username}.tumblr.com/js"></script>
不交电费瞎发啥光2024-08-14 14:08:22

您可以使用 PHPTumblr,这是一个用 PHP 编写的 API 包装器,可以让检索帖子变得轻而易举。

You can use PHPTumblr, an API wrapper written in PHP which makes retrieving posts a breeze.

过去的过去2024-08-14 14:08:22

如果您访问 http://yourblog.tumblr.com/api/read 其中“yourblog”应替换为您博客的名称(请注意,如果您将 Tumblr 博客托管在自定义域上,就像我一样,请使用该名称)您将看到博客的 XML 版本。由于某种原因,它在 Firefox 上对我来说真的很混乱,所以我使用 Chrome,尝试几个不同的浏览器,这将有助于查看格式正确、缩进等的 XML 文件。

查看博客的 XML 版本后,请注意每个帖子都有一堆属性=“值”方向的数据。这是我博客中的一个示例:

<post id="11576453174" url="http://wamoyo.com/post/11576453174" url-with-slug="http://wamoyo.com/post/11576453174/100-year-old-marathoner-finishes-race" type="link" date-gmt="2011-10-17 18:01:27 GMT" date="Mon, 17 Oct 2011 14:01:27" unix-timestamp="1318874487" format="html" reblog-key="E2Eype7F" slug="100-year-old-marathoner-finishes-race" bookmarklet="true">

因此,有很多方法可以做到这一点,我将向您展示我使用的方法,并将我的代码放在本文的底部,以便您可以根据自己的需要进行定制。注意到 type="link" 部分了吗?或者 id="11576453174" ?这些是您将用来将数据提取到 PHP 脚本中的值。

例子如下:

<!-- The Latest Text Post -->
<?php
    echo "";
    $request_url = "http://wamoyo.com/api/read?type=regular"; //get xml file
    $xml = simplexml_load_file($request_url); //load it
    $title = $xml->posts->post->{'regular-title'}; //load post title into $title
    $post = $xml->posts->post->{'regular-body'}; //load post body into $post
    $link = $xml->posts->post['url']; //load url of blog post into $link
    $small_post = substr($post,0,350); //shorten post body to 350 characters
    echo // spit that baby out with some stylish html
        '<div class="panel" style="width:220px;margin:0 auto;text-align:left;">
            <h1 class="med georgia bold italic black">'.$title.'</h1>'
            . '<br />' 
            . '<span>'.$small_post.'</span>' . '...' 
            . '<br /></br><div style="text-align:right;"><a class="bold italic blu georgia" href="'.$link.'">Read More...</a></div>
        </div>
        <img style="position:relative;top:-6px;" src="pic/shadow.png" alt="" />
    '; 
?>

所以,这实际上相当简单。这里的 PHP 脚本将 xml 文件中的数据(如帖子标题和帖子文本)放入 php 变量中,然后回显这些变量以及一些 html 以创建一个包含博客文章片段的 div。本文以最新的文本帖子为特色。请随意使用它,只需进入并将第一个网址更改为您自己的博客即可。然后从 xml 文件中选择您想要的任何值。

例如,假设您想要的不是最新的,而是第二个最新的“照片”帖子。您必须将 request_url 更改为:

$request_url = "http://wamoyo.com/api/read?type=photo&start=1"

或者假设您想要带有特定标签的最新帖子

$request_url = "http://wamoyo.com/api/read?tagged=events";

或者假设您想要特定帖子,只需使用 id

$request_url = "http://wamoyo.com/api/read?id=11576453174";

那么您所要做的就是添加?与任何参数并使用 &如果你有多个参数。

如果你想做一些更奇特的事情,你需要这里的 tumblr api 文档:http:// /www.tumblr.com/docs/en/api/v2

希望这对您有帮助!

If you go to http://yourblog.tumblr.com/api/read where "yourblog" should be replaced with the name of your blog (be careful, if you host your Tumblr blog on a custom domain, like I do, use that) you'll see the XML version of your blog. It comes up really messy for me on Firefox for some reason so I use Chrome, try a couple of different browser, it'll help to see the XML file well-formed, indented and such.

Once your looking at the XML version of your blog, notice that each post has a bunch of data in an attribute="value" orientation. Here's an example from my blog:

<post id="11576453174" url="http://wamoyo.com/post/11576453174" url-with-slug="http://wamoyo.com/post/11576453174/100-year-old-marathoner-finishes-race" type="link" date-gmt="2011-10-17 18:01:27 GMT" date="Mon, 17 Oct 2011 14:01:27" unix-timestamp="1318874487" format="html" reblog-key="E2Eype7F" slug="100-year-old-marathoner-finishes-race" bookmarklet="true">

So, there's lots of ways to do this, I'll show you the one I used, and drop my code on the bottom of this post so you can just tailor that to your needs. Notice the type="link" part? Or the id="11576453174" ? These are the values you're going to use to pull data into your PHP script.

Here's the example:

<!-- The Latest Text Post -->
<?php
    echo "";
    $request_url = "http://wamoyo.com/api/read?type=regular"; //get xml file
    $xml = simplexml_load_file($request_url); //load it
    $title = $xml->posts->post->{'regular-title'}; //load post title into $title
    $post = $xml->posts->post->{'regular-body'}; //load post body into $post
    $link = $xml->posts->post['url']; //load url of blog post into $link
    $small_post = substr($post,0,350); //shorten post body to 350 characters
    echo // spit that baby out with some stylish html
        '<div class="panel" style="width:220px;margin:0 auto;text-align:left;">
            <h1 class="med georgia bold italic black">'.$title.'</h1>'
            . '<br />' 
            . '<span>'.$small_post.'</span>' . '...' 
            . '<br /></br><div style="text-align:right;"><a class="bold italic blu georgia" href="'.$link.'">Read More...</a></div>
        </div>
        <img style="position:relative;top:-6px;" src="pic/shadow.png" alt="" />
    '; 
?>

So, this is actually fairly simple. The PHP script here places data (like the post title and post text) from the xml file into php variables, and then echos out those variable along with some html to create a div which features a snippet from a blog post. This one features the most recent text post. Feel free to use it, just go in and change that first url to your own blog. And then choose whatever values you want from your xml file.

For example let's say you want, not the most recent, but the second most recent "photo" post. You have to change the request_url to this:

$request_url = "http://wamoyo.com/api/read?type=photo&start=1"

Or let's say you want the most recent post with a specific tag

$request_url = "http://wamoyo.com/api/read?tagged=events";

Or let's say you want a specific post, just use the id

$request_url = "http://wamoyo.com/api/read?id=11576453174";

So all you have to do is tack on the ? with whatever parameter and use an & if you have multiple parameters.

If you want to do something fancier, you'll need the tumblr api docs here: http://www.tumblr.com/docs/en/api/v2

Hope this was helpful!

日久见人心2024-08-14 14:08:22

有两种主要方法可以做到这一点。首先,您可以解析 xml,从您需要的标签中提取内容(有几种方法可以完成此操作,具体取决于您使用的是 SAX 还是 DOM 解析器)。这是快速但肮脏的解决方案。

您还可以使用 XSLT 转换 将 xml 源直接转换为您想要的 html。这更加复杂,因为您必须学习 xslt 模板的语法,这有点冗长。

There are two main ways to do this. First, you can parse the xml, pulling out the content from the the tags you need (a few ways to do this depending on whether you use a SAX or DOM parser). This is the quick and dirty solution.

You can also use an XSLT transformation to convert the xml source directly to the html you want. This is more involved since you have to learn the syntax for xslt templates, which is a bit verbose.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文