越小<content:encoded>比

时间:2016-03-12 20:55:44

标签: php wordpress rss

我试图获取带有标记的RSS提要,但总是会减少到文本的一小部分继续&#34; ...&#34;。 我已经检查了this topic但是没有达到我预期的结果。 这是我尝试解析的Feed:

https://bus237d201smartphones.wordpress.com/feed/

这是我的代码:

function resolveFile($file_or_url) {
    if (!preg_match('|^https?:|', $file_or_url))
        $feed_uri = $_SERVER['DOCUMENT_ROOT'] .'/shared/xml/'. $file_or_url;
    else
        $feed_uri = $file_or_url;

    return $feed_uri;
}

$file_or_url='https://bus237d201smartphones.wordpress.com/feed/';
$file_or_url = resolveFile($file_or_url);
if (!($x = simplexml_load_file($file_or_url)))
    return;

foreach ($x->channel->item as $item)
{
    $content  = $item->children("content", true);;
    echo("<br>");
    echo("content <br>");
    print_r($content);
    $e_encoded     = $content->encoded;
    echo("<br>");
    echo("encoded <br>");
    print_r($e_encoded);
}

这是浏览器输出:

content 
SimpleXMLElement Object ( [encoded] => SimpleXMLElement Object ( ) ) 
encoded 
SimpleXMLElement Object ( [0] => SimpleXMLElement Object ( ) ) 
    Google made the Android operating system for all kinds
    of handheld devices (Smartphones and PC Table...

它出了什么问题?

1 个答案:

答案 0 :(得分:1)

如果您尝试获取内容为名称空间<content:encoded>的元素xmlns:content="http://purl.org/rss/1.0/modules/content/"的内容,则可以使用以下代码执行此操作:

<?php

function resolveFile($file_or_url) {
    if (!preg_match('|^https?:|', $file_or_url))
        $feed_uri = $_SERVER['DOCUMENT_ROOT'] .'/shared/xml/'. $file_or_url;
    else
        $feed_uri = $file_or_url;

    return $feed_uri;
}

$file_or_url='https://bus237d201smartphones.wordpress.com/feed/';
$file_or_url = resolveFile($file_or_url);
if (!($x = simplexml_load_file($file_or_url)))
    return;

foreach ( $x->getNameSpaces( true ) as $key => $children ) {
    $$key = $x->children($children);
}

foreach ($x->channel->item as $item)
{
    $childrenContent = $item->children('http://purl.org/rss/1.0/modules/content/');
    $encodedContent  = $childrenContent->encoded->__toString();
    echo("<br>");
    echo("encoded <br>");
    print_r($encodedContent);
}
相关问题