Php SimpleXML可以';无法获取带有ns前缀的CDATA

Php SimpleXML可以';无法获取带有ns前缀的CDATA,php,xml,cdata,xenforo,Php,Xml,Cdata,Xenforo,在过去的几个小时里,我一直在努力从一个xml文件中获取CDATA,尽管我尝试了不同的方法 我的困境与通过xenForo的RSS提要检索线程数据有关。这是我试图检索的RSS数据的一个示例,除了检索之外,一切正常 示例文件: <?xml version="1.0" encoding="utf-8"?> <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/el

在过去的几个小时里,我一直在努力从一个xml文件中获取CDATA,尽管我尝试了不同的方法

我的困境与通过xenForo的RSS提要检索线程数据有关。这是我试图检索的RSS数据的一个示例,除了检索
之外,一切正常

示例文件:

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>News &amp; Announcements</title>
    <description>All of our important news and announcements will be here.</description>
    <pubDate>Fri, 26 Jun 2015 14:54:20 +0000</pubDate>
    <lastBuildDate>Fri, 26 Jun 2015 14:54:20 +0000</lastBuildDate>
    <generator>********* ****</generator>
    <link>https://***.****.****/forum/news/</link>
    <atom:link rel="self" type="application/rss+xml" href="https://***.****.****/forum/news/index.rss"/>
    <item>
      <title>Site under development.</title>
      <pubDate>Thu, 25 Jun 2015 05:49:43 +0000</pubDate>
      <link>https://***.****.****/threads/site-under-development.3/</link>
      <guid>https://***.****.****/threads/site-under-development.3/</guid>
      <author>invalid@example.com (*****)</author>
      <dc:creator>ShortCut Central</dc:creator>
      <content:encoded><![CDATA[Content to retrieve. <br /> Some more content a part of the same section]]></content:encoded>
    </item>
  </channel>
</rss>

新闻及;公告
我们所有的重要新闻和公告都会在这里。
2015年6月26日星期五14:54:20+0000
2015年6月26日星期五14:54:20+0000
********* ****
https://************/论坛/新闻/
正在开发的网站。
2015年6月25日星期四05:49:43+0000
https://************/threads/正在开发的站点。3/
https://************/threads/正在开发的站点。3/
invalid@example.com (*****)
捷径中心
同一部分的其他内容]]>
我当前的代码看起来像

<?php
class SCC_Main_miscFuncs {
    public static function printMostRecentPost() {
        // Re-enable the below once we're ready to release
        //$rssUrl = func_get_arg(1);
        $rssUrl = 'https://www.shortcutcentral.org/indev.rss';
        $xml = simplexml_load_string(self::returnContents($rssUrl));
        $rawData = self::returnContents($rssUrl); // Properly contains the CDATA
        echo '<pre>';
        //echo (string) $xml->channel->item->encoded;
        //echo (string) $xml->channel->item->content;
        //var_dump($xml);
        echo '</pre>';
        //echo (string) $xml->channel->item;
        //echo $array[@attributes]['item']['link'];
        //echo $xml->message;
    }

    public static function returnContents($url){
        $curl_handle=curl_init();
        curl_setopt($curl_handle, CURLOPT_URL,$url);
        curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
        curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl_handle, CURLOPT_USERAGENT, 'ShortCut Central');
        $query = curl_exec($curl_handle);
        curl_close($curl_handle);
        return $query;
    }
} 

在SimpleXML中返回名称空间节点的一个方法是使用,这是正确的,但必须将名称空间作为其第一个参数传递。您可以传递完整的命名空间字符串
“http://purl.org/rss/1.0/modules/content/“
,但传递其前缀
”content“
,然后提供
TRUE
作为第二个参数来通知
children()

因此,在
$xml
对象上使用表达式,如:

echo (string)$xml->channel->children('atom', true)->link->attributes()['href'];
// Prints
// https://***.****.****/forum/news/index.rss

你的数据是什么样子的?@GordonM是指向不适合你的样本的链接吗?(点击上面的示例)@Adam您应该包含所有相关代码作为有效的最低示例。外部网站可能会改变,链接会被破坏,如果有人想从你的问题和它的内容中学习,那么你的问题在将来就没有意义了answers@Michi我会把它包括在内,但我曾经看到人们因为在他们的帖子中包含代码墙而受到抨击,所以…@Adam做得好,有效的最小示例是关键,没有代码墙,谢谢!今天晚些时候我会尝试这个,一旦我完成了,我会把你的答案标记为有效答案。
echo (string)$xml->channel->children('atom', true)->link->attributes()['href'];
// Prints
// https://***.****.****/forum/news/index.rss