仅限使用 PHP 标记的帖子

发布于 2024-11-26 01:26:18 字数 625 浏览 3 评论 0原文

我试图只获取 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 技术交流群。

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

发布评论

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

评论(1

流绪微梦 2024-12-03 01:26:18

首先,您需要将标记放在属于 foreach 循环的大括号内。否则,您引用的 $post 对象的范围将不可用,并且会出现错误。

这就是我想到的:

<ul>
<?php

$xml = simplexml_load_file('http://stocklamp.tumblr.com/api/read');
$posts = $xml->xpath("/tumblr/posts/post/tag/..");
foreach($posts as $post) {
    echo "
  <li>
   <a href='{$post['url-with-slug']}'>".($post->{'regular-title'}?$post->{'regular-title'}:$post->{'link-text'})."</a>
  </li>
";
}

?>
</ul>

如果你想限制匹配的标签,你可以执行以下操作:

<ul>
<?php

$xml = simplexml_load_file('http://stocklamp.tumblr.com/api/read');
$posts = $xml->xpath("/tumblr/posts/post/tag[contains(text(),'xml') or contains(text(),'php') or contains(text(),'firefox')]/..");
foreach($posts as $post) {
    echo "
  <li>
   <a href='{$post['url-with-slug']}'>".($post->{'regular-title'}?$post->{'regular-title'}:$post->{'link-text'})."</a>
  </li>
";
}

?>
</ul>

当然,这有点混乱。我不一定是 XPath 专家,所以应该有比我想象的更干净的方法。

由于我并没有真正挖掘链接文本的三元,或者事实上我不知道其中一个或另一个是否总是可用,所以这应该更具描述性,并回退到 URL。

<ul>
<?php

$xml = simplexml_load_file('http://stocklamp.tumblr.com/api/read');
$posts = $xml->xpath("/tumblr/posts/post/tag[contains(text(),'xml') or contains(text(),'php') or contains(text(),'firefox')]/..");
foreach($posts as $post) {
    if ($post->{'regular-title'}) {
        $slug = $post->{'regular-title'};
    } else if ($post->{'link-text'}) {
        $slug = $post->{'link-text'};
    } else {
        $slug = $post['url-with-slug'];
    }
    echo "
  <li>
   <a href='{$post['url-with-slug']}'>".$slug."</a>
  </li>
";
}

?>
</ul>

编辑

而且,您可能还希望对 TAG 元素的整个文本进行匹配,这样您就不会在 xml- 等内容上获得匹配项is-for-noobs 当您只需要 xml 标签时。 contains() 将匹配两者,而下面的代码将仅匹配 xml

/tumblr/posts/post/tag[text() = 'xml']/..

因此,根据您的评论,如果您正在寻找标记为 events 的帖子,但不想捕获诸如 bowling-events 之类的标签,会妨碍乐趣 code>、其他事件等...,您可能希望使 text() = 'events' 匹配。另一方面,如果您确实想要将 TAG 节点文本中的任何内容与 events 进行匹配,请使用 contains(text(), 'events' ) 方法。

编辑2

如果您想限制结果,请使用position()

(/tumblr/posts/post/tag[text() = 'xml']/..)[position() <= 3]

注意,我网页上的示例不包含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:

<ul>
<?php

$xml = simplexml_load_file('http://stocklamp.tumblr.com/api/read');
$posts = $xml->xpath("/tumblr/posts/post/tag/..");
foreach($posts as $post) {
    echo "
  <li>
   <a href='{$post['url-with-slug']}'>".($post->{'regular-title'}?$post->{'regular-title'}:$post->{'link-text'})."</a>
  </li>
";
}

?>
</ul>

And if you want to limit the tags matched, you can do the following:

<ul>
<?php

$xml = simplexml_load_file('http://stocklamp.tumblr.com/api/read');
$posts = $xml->xpath("/tumblr/posts/post/tag[contains(text(),'xml') or contains(text(),'php') or contains(text(),'firefox')]/..");
foreach($posts as $post) {
    echo "
  <li>
   <a href='{$post['url-with-slug']}'>".($post->{'regular-title'}?$post->{'regular-title'}:$post->{'link-text'})."</a>
  </li>
";
}

?>
</ul>

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.

<ul>
<?php

$xml = simplexml_load_file('http://stocklamp.tumblr.com/api/read');
$posts = $xml->xpath("/tumblr/posts/post/tag[contains(text(),'xml') or contains(text(),'php') or contains(text(),'firefox')]/..");
foreach($posts as $post) {
    if ($post->{'regular-title'}) {
        $slug = $post->{'regular-title'};
    } else if ($post->{'link-text'}) {
        $slug = $post->{'link-text'};
    } else {
        $slug = $post['url-with-slug'];
    }
    echo "
  <li>
   <a href='{$post['url-with-slug']}'>".$slug."</a>
  </li>
";
}

?>
</ul>

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 like xml-is-for-noobs when you want xml tags only. contains() will match both, while the below will only match xml.

/tumblr/posts/post/tag[text() = 'xml']/..

So, according to your comment, if you're looking for posts tagged events but don't want to catch tags like bowling-events, prevents fun, other events, etc..., you would want to make the text() = 'events' match. On the other hand, if you DID want to match anything with events in the text of the TAG node, use the contains(text(), 'events') method.

EDIT 2

And if you're trying to limit the results, use position():

(/tumblr/posts/post/tag[text() = 'xml']/..)[position() <= 3]

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 a POST element, then selects the parent (..) of that TAG, which gives you a SimpleXML object containing the relevant POST elements you're looking for.

Then, it iterates over each element, printing the $post's information into a UL element.

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