3

这个问题有两个部分。

第 1 部分。昨天我有一些代码可以回显来自 RSS 提要的 XML 的全部内容。然后我从我的 php 文档中删除它,保存它,我完全在踢自己。

我相信语法是这样的:

$xml = simplexml_load_file($url);
echo $xml;

我又试了一次,但它不起作用,所以显然我忘记了正确的语法,可以使用你的帮助,亲爱的 stackoverflow 问答者。

我一直试图弄清楚我在做什么,但我无法在 Google 或 PHP 网站上找到示例。我试过 print_r($url); 命令,它给了我似乎是一个原子化版本的提要。我想要整个字符串,疣和所有。我意识到我可以在窗口中键入 RSS 链接并查看它,但是在我编码和点头时将它放在我的 PHP 页面上很有帮助。

第 2 部分我想重建它的主要原因是因为我试图从博客 RSS 中解析节点,以便将其显示在托管在私有域上的网页上。我发布了一个虚拟博客,当我未能为其中一个虚拟帖子添加标题时,发现了一个尴尬的格式错误。

那么在这种情况下该怎么办呢?我试了一下:

if(entry->title == "")
{$entryTitle = "untitled";}

那根本行不通。

这是我处理博客的整个 php 脚本:

<?php
/*create variables*/
$subtitle ="";
$entryTitle="";
$html = "";
$pubDate ="";
/*Store RSS feed address in new variable*/
$url = "http://www.blogger.com/feeds/6552111825067891333/posts/default";
/*Retrieve BLOG XML and store it in PHP object*/
$xml = simplexml_load_file($url);
print_r($xml);
/*Parse blog subtitle into HTML and echo it on the page*/
$subtitle .= "<h2 class='blog'>" . $xml->subtitle . "</h2><br />";
echo $subtitle;
/*Go through all the entries and parse them into HTML*/
foreach($xml->entry as $entry){
/*retrieve publication date*/
    $xmlDate = $entry->published;
    /*Convert XML timestamp into PHP timestamp*/
    $phpDate = new DateTime(substr($xmlDate,0,19));
    /*Format PHP timestamp to something humans understand*/
    $pubDate .= $phpDate->format('l\, F j\, Y h:i A');
    if ($entry->title == "")
    {
        $entryTitle .= "Untitled";
    }
        echo $entry->title;
    /*Pick through each entry and parse each XML tree node into an HTML ready blog post*/
        $html .= "<h3 class='blog'>".$entry->title . "<span class='pubDate'> | " .$pubDate . "</span></h3><p class='blog'>" . $entry->content . "</p>";
    /*Print the HTML to the web page*/  
        echo $html;
    /*Set the variables back to empty strings so they do not repeat data upon reiteration*/
        $html = "";
        $pubDate = "";
}
?>
4

4 回答 4

12

根据php手册:

$xml = new SimpleXMLElement($string);
.
.
.

那么如果你想要echo结果:

echo $xml->asXML();

或将 xml 保存到文件中:

$xml->asXML('blog.xml'); 

参考

于 2013-12-22T11:48:32.827 回答
2

第1部分

这仍然不是我想要的,而是一种非常整洁和有组织的方式来回显 xml 数据:

    $url = "http://www.blogger.com/feeds/6552111825067891333/posts/default";
$xml = simplexml_load_file($url);
echo '<pre>';
print_r($xml);

第2部分

我必须让 firephp 运行,这样我才能准确地看到 php 在到达没有博客标题的条目时遇到的元素。最终它是一个空数组。因此,简单的:

if(empty($entry->title))

完美运行。对于字符串比较,我发现您可以简单地将其转换为字符串。就我的目的而言,这是不必要的。

于 2012-06-13T17:06:48.213 回答
1

simplexml_load_file返回一个SimpleXMLElement,所以:

print_r($xml);

将显示其次要对象数组

调整后,您可以调用$xml->asXML("filename.xml"); 正如@Tim Withers 指出的那样。

于 2012-06-12T23:52:51.863 回答
0

第 1 部分:echo $xml->asXML();- http://www.php.net/manual/en/simplexmlelement.asxml.php

第 2 部分:php SimpleXML 检查子项是否存在

$html .= "<h3 class='blog'>".($entry->title!=null?$entry->title:'No Title')
    . "<span class='pubDate'> | " .$pubDate . "</span></h3><p class='blog'>" 
    . $entry->content . "</p>";

注意我可能会像这样加载网址:

$feedUrl = 'http://www.blogger.com/feeds/6552111825067891333/posts/default';
$rawFeed = file_get_contents($feedUrl);
$xml = new SimpleXmlElement($rawFeed);

根据您对第 1 部分的评论,我不确定 XML 是否已完全加载。如果您尝试以这种方式加载它,它应该会显示所有 XML 数据。

于 2012-06-12T23:52:08.900 回答