如何使用PHP从tumblr帖子获取标签和日期?(How to get tags and the date from tumblr posts using php?)

编程入门 行业动态 更新时间:2024-10-26 09:21:36
如何使用PHP从tumblr帖子获取标签和日期?(How to get tags and the date from tumblr posts using php?)

我是PHP的新手,我设法在我的网站上获得tumblr帖子。 但只有标题和身体。

1.)为什么我不能抓住约会? 2.)如何浏览所有标签并将其导出?

这是我的代码:

<?php ini_set('display_errors', true); error_reporting(-1); $request_url = 'http://cedricfjacob.tumblr.com/api/read?type=post&start=0&num=10'; $xml = simplexml_load_file($request_url); $posts = $xml->xpath("/tumblr/posts/post"); foreach($posts as $post) { $title = $post->{'regular-title'}; $post = $post->{'regular-body'}; $tag = $post->{'tag'}; $date = $post['date']; echo '<div class="title">'.$title.'</div>'; echo '<div class="date">'.$date.'</div>'; echo '<div class="tags">'.$tag.'</div>'; echo '<div class="post">'.$post.'</div>'; }?>

要查看XML,请访问:

https://cedricfjacob.tumblr.com/api/read?type=post&start=0&num=10# =

感谢你们。

I am rather new to PHP and I managed to get tumblr posts on my website. But only the title and the body.

1.) Why is it that I cannot grab the date? 2.) How do I go through all the tags and export them as well?

Here is my code:

<?php ini_set('display_errors', true); error_reporting(-1); $request_url = 'http://cedricfjacob.tumblr.com/api/read?type=post&start=0&num=10'; $xml = simplexml_load_file($request_url); $posts = $xml->xpath("/tumblr/posts/post"); foreach($posts as $post) { $title = $post->{'regular-title'}; $post = $post->{'regular-body'}; $tag = $post->{'tag'}; $date = $post['date']; echo '<div class="title">'.$title.'</div>'; echo '<div class="date">'.$date.'</div>'; echo '<div class="tags">'.$tag.'</div>'; echo '<div class="post">'.$post.'</div>'; }?>

In order to see the XML, please visit:

https://cedricfjacob.tumblr.com/api/read?type=post&start=0&num=10#=

Thank you guys.

最满意答案

这是因为两个原因:

使用$post = $post->{'regular-body'}; 你写了 $post值。 它不再是你所期望的。

第二个 - 属性$post['date']是一个SimpleXMLElement对象。 要获取它的字符串值 - 例如使用strval或使用(string)类型转换。

到底:

foreach($posts as $post) { $title = $post->{'regular-title'}; $post_body = $post->{'regular-body'}; // change here $tag = $post->{'tag'}; $date = strval($post['date']); // change here echo '<div class="title">'.$title.'</div>'; echo '<div class="date">'.$date.'</div>'; echo '<div class="tags">'.$tag.'</div>'; echo '<div class="post">'.$post_body.'</div>'; // change here }

This happens because of two reasons:

With $post = $post->{'regular-body'}; you ovewrite $post value. It is no longer what you expect.

Second - attribute $post['date'] is a SimpleXMLElement object. To get string value of it - use strval for example or (string) typecasting.

In the end:

foreach($posts as $post) { $title = $post->{'regular-title'}; $post_body = $post->{'regular-body'}; // change here $tag = $post->{'tag'}; $date = strval($post['date']); // change here echo '<div class="title">'.$title.'</div>'; echo '<div class="date">'.$date.'</div>'; echo '<div class="tags">'.$tag.'</div>'; echo '<div class="post">'.$post_body.'</div>'; // change here }

更多推荐

本文发布于:2023-08-07 23:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466026.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   日期   标签   帖子   tumblr

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!