php 将 tumblr 博客与网站集成
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1568210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Integrating tumblr blog with website
提问by Ying
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?
我想将我的 tumblr 提要集成到我的网站中。tumblr 似乎有一个 API,但我不太确定如何使用它。据我了解,我请求页面,tumblr 返回一个包含我博客内容的 xml 文件。但是我如何将这个 xml 变成有意义的 html?我是否必须用 php 解析它,将相关标签转换为标题等等?我告诉自己这不可能那么痛苦。有人有任何见解吗?
采纳答案by ChrisR
回答by kweerious
There's a javascript include that does this now, available from Tumblr (you have to login to see it): http://www.tumblr.com/developers
现在有一个 javascript include 可以做到这一点,可以从 Tumblr 获得(你必须登录才能看到它):http: //www.tumblr.com/developers
It winds up being something like this:
它最终是这样的:
<script type="text/javascript" src="http://{username}.tumblr.com/js"></script>
回答by Costa
If you go to http://yourblog.tumblr.com/api/readwhere "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.
如果您访问http://yourblog.tumblr.com/api/read,其中“yourblog”应替换为您的博客名称(请注意,如果您像我一样在自定义域上托管您的 Tumblr 博客,请使用那)您将看到您博客的 XML 版本。由于某种原因,它在 Firefox 上对我来说真的很混乱,所以我使用 Chrome,尝试几个不同的浏览器,这将有助于查看 XML 文件格式正确、缩进等。
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:
一旦您查看了博客的 XML 版本,请注意每篇文章都有一堆以 attribute="value" 方向的数据。这是我博客中的一个例子:
<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.
因此,有很多方法可以做到这一点,我将向您展示我使用过的方法,并将我的代码放在这篇文章的底部,以便您可以根据自己的需要进行调整。注意到 type="link" 部分了吗?还是 id="11576453174" ?这些是您将用于将数据拉入 PHP 脚本的值。
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.
所以,这实际上相当简单。此处的 PHP 脚本将来自 xml 文件的数据(如帖子标题和帖子文本)放入 php 变量中,然后将这些变量与一些 html 一起回显以创建一个 div,该 div 具有来自博客文章的片段。这一篇以最新的文字帖子为特色。随意使用它,只需进入并将第一个 url 更改为您自己的博客。然后从您的 xml 文件中选择您想要的任何值。
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 更改为:
$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
或者假设您想要一个特定的帖子,只需使用 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
如果你想做一些更有趣的事情,你需要这里的 tumblr api 文档:http: //www.tumblr.com/docs/en/api/v2
Hope this was helpful!
希望这是有帮助的!
回答by Dana the Sane
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.
有两种主要方法可以做到这一点。首先,您可以解析 xml,从您需要的标签中提取内容(执行此操作的几种方法取决于您使用的是 SAX 还是 DOM 解析器)。这是快速而肮脏的解决方案。
You can also use an XSLT transformationto 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.
您还可以使用XSLT 转换将 xml 源直接转换为您想要的 html。这更复杂,因为您必须学习 xslt 模板的语法,这有点冗长。

