将 tumblr 博客与网站集成
我想将我的 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 技术交流群。

发布评论
评论(4)
如果您访问 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
希望这对您有帮助!
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
现在有一个 javascript include 可以执行此操作,可从 Tumblr 获取(您必须登录才能看到它): http://www .tumblr.com/developers
它最终是这样的:
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: