仅限使用 PHP 标记的帖子
我试图只获取 tumblr 博客上标记的帖子。我可以根据帖子的类型(常规、链接、照片等)获取每个帖子,但我想获取每个带有特定标签的帖子。例如,仅获取标记为“文件”的帖子
这是迄今为止我的代码:
<?php
$xml = simplexml_load_file('http://stocklamp.tumblr.com/api/read');
$posts = $xml->xpath("/tumblr/posts/post[@type='photo']");
foreach($posts as $post) {
}
?>
<ul>
<li> <a href="<?php echo $post['url-with-slug']; ?>">Post 1(PHP should be here)</a></li>
</ul>
这是 tumblrs API http://www.tumblr.com/docs/en/api/v2
I am trying to only get the posts that are tagged on a tumblr blog. I can get each post depending on what type of post it is (regular, link, photo, etc) but instead, I want to get each post that has a certain tag on it. For example, only get the posts that are tagged 'file'
Here is my code so far:
<?php
$xml = simplexml_load_file('http://stocklamp.tumblr.com/api/read');
$posts = $xml->xpath("/tumblr/posts/post[@type='photo']");
foreach($posts as $post) {
}
?>
<ul>
<li> <a href="<?php echo $post['url-with-slug']; ?>">Post 1(PHP should be here)</a></li>
</ul>
and here is tumblrs API http://www.tumblr.com/docs/en/api/v2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您需要将标记放在属于
foreach
循环的大括号内。否则,您引用的$post
对象的范围将不可用,并且会出现错误。这就是我想到的:
如果你想限制匹配的标签,你可以执行以下操作:
当然,这有点混乱。我不一定是 XPath 专家,所以应该有比我想象的更干净的方法。
由于我并没有真正挖掘链接文本的三元,或者事实上我不知道其中一个或另一个是否总是可用,所以这应该更具描述性,并回退到 URL。
编辑
而且,您可能还希望对
TAG
元素的整个文本进行匹配,这样您就不会在xml- 等内容上获得匹配项is-for-noobs
当您只需要xml
标签时。contains()
将匹配两者,而下面的代码将仅匹配xml
。因此,根据您的评论,如果您正在寻找标记为
events
的帖子,但不想捕获诸如bowling-events
之类的标签,会妨碍乐趣
code>、其他事件
等...,您可能希望使text() = 'events'
匹配。另一方面,如果您确实想要将TAG
节点文本中的任何内容与events
进行匹配,请使用contains(text(), 'events' )
方法。编辑2
如果您想限制结果,请使用
position()
:注意,我网页上的示例不包含
text()< /code> 匹配,因为没有足够的条目来支持子匹配并且仍然有效。然而,这应该有效。
http://jfcoder.com/test/tumblrtest.php
XPath选择所有
TAG属于
元素,然后选择该POST
元素的TAG
的父级 (..
),这会为您提供 SimpleXML包含相关的对象您要查找的POST
元素。然后,它迭代每个元素,将
$post
的信息打印到UL
元素中。First, you'll need to have your markup within the curly braces that belong to the
foreach
loop. Otherwise, the scope of the$post
object you're referencing will not be available, and it give you an error.This is what I came up with:
And if you want to limit the tags matched, you can do the following:
Of course, that's a little messy. I'm not necessarily an XPath guru, so there should be a cleaner way than that I would think.
And since I wasn't really digging on the ternary for the link text, or the fact I don't know if one or the other will always be available, this should be more descriptive with a fallback to the url.
EDIT
And, you also might want to make a match of the entire text of the
TAG
element, so that you don't get matches on things likexml-is-for-noobs
when you wantxml
tags only.contains()
will match both, while the below will only matchxml
.So, according to your comment, if you're looking for posts tagged
events
but don't want to catch tags likebowling-events
,prevents fun
,other events
, etc..., you would want to make thetext() = 'events'
match. On the other hand, if you DID want to match anything withevents
in the text of theTAG
node, use thecontains(text(), 'events')
method.EDIT 2
And if you're trying to limit the results, use
position()
:Note, the example on my webpage does not include the
text()
match, since there are not enough entries to support a submatch and still be productive. However, that should work.http://jfcoder.com/test/tumblrtest.php
The XPath selects all
TAG
elements that belong to aPOST
element, then selects the parent (..
) of thatTAG
, which gives you a SimpleXML object containing the relevantPOST
elements you're looking for.Then, it iterates over each element, printing the
$post
's information into aUL
element.