我是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 }更多推荐
发布评论