使用PHP解析Facebook atom feed的问题

时间:2011-08-02 22:56:42

标签: php facebook parsing feed atom-feed

我在解析原子facebook Feed时遇到问题。

使用:

PHP的DOMDocument。

错误:

  

警告:DOMDocument :: loadXML():xmlParseEntityRef:实体中没有名称,第12行:   警告:DOMDocument :: loadXML():xmlParseEntityRef:实体中没有名称,行:12   警告:DOMDocument :: loadXML():实体'euro'未在Entity中定义,第16行   警告:DOMDocument :: loadXML():实体'acute'未在Entity中定义,第16行   警告:DOMDocument :: loadXML():实体'euro'未在Entity中定义,第16行   警告:DOMDocument :: loadXML():实体'acute'未在Entity中定义,第16行   注意:尝试在第76行的......中获取非对象的属性

不幸的是,在Feed代码中找不到上面提到的实体。不那么简单......问题必须是不同的。可以使用相同的代码解析其他提要,而不会出现任何问题。所以,我认为问题是内容标签中Facebook的HTML中存在的问题。 这可能是什么?如何解决?

    <content type="html">&lt;div class=&quot;ext_media clearfix has_extra has_thumb&quot;&gt;&lt;div class=&quot;title&quot;&gt;&lt;a href=&quot;http://www.youtube.com/watch?v=BPq58p0K6DM&amp;feature=youtu.be&quot; target=&quot;_blank&quot; rel=&quot;nofollow&quot; title=&quot;http://www.youtube.com/watch?v=BPq58p0K6DM&amp;amp;feature=youtu.be&quot; onmousedown=&quot;UntrustedLink.bootstrap($(this), &quot;-AQBiGfHA&quot;, event, bagof(null));&quot;&gt;Did you know there were this many satellites in orbit VIDEO&lt;/a&gt;&lt;/div&gt;&lt;div class=&quot;url&quot;&gt;Quelle: www.youtube.com&lt;/div&gt;&lt;div class=&quot;story_posted_item clearfix&quot;&gt;&lt;div class=&quot;extra&quot;&gt;&lt;div class=&quot;share_thumb&quot;&gt;&lt;a href=&quot;http://www.youtube.com/watch?v=BPq58p0K6DM&amp;feature=youtu.be&quot; target=&quot;_blank&quot; rel=&quot;nofollow&quot; onmousedown=&quot;UntrustedLink.bootstrap($(this), &quot;2AQBCjOTV&quot;, event, bagof(null));&quot;&gt;&lt;img class=&quot;img_loading img&quot; src=&quot;http://i3.ytimg.com/vi/BPq58p0K6DM/default.jpg&quot; alt=&quot;&quot; onload=&quot;var img = this; onloadRegister(function() { adjustImage(img); });&quot; id=&quot;share_thumb_257759307568958&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</content>

查看完整供稿代码 here。 (参见Feed in browser。)

我正在使用的完整PHP代码:

    $feed_xml_str = ...;

print '<pre>';
print_r( xmlstr_to_array($feed_xml_str) );
print '</pre>';

function xmlstr_to_array($xmlstr) {
    $doc = new DOMDocument();
    $doc->loadXML($xmlstr);
    return domnode_to_array($doc->documentElement);
}
function domnode_to_array($node) {
    $output = array();
    switch ($node->nodeType) {
        case XML_CDATA_SECTION_NODE:
        case XML_TEXT_NODE:
            $output = trim($node->textContent);
            break;
        case XML_ELEMENT_NODE:
            for ($i=0, $m=$node->childNodes->length; $i<$m; $i++) {
                $child = $node->childNodes->item($i);
                $v = domnode_to_array($child);
                if(isset($child->tagName)) {
                    $t = $child->tagName;
                    if(!isset($output[$t])) {
                        $output[$t] = array();
                    }
                    $output[$t][] = $v;
                }
                elseif($v) {

                    // >>>>> WJ: OUT COMMENTED CODE >>>>>
                    //$output = (string) $v;
                    // >>>>> WJ: ADDED CODE >>>>>
                    if($node->attributes->length) {
                        $a = array();
                        foreach($node->attributes as $attrName => $attrNode) {
                            $a[$attrName] = (string) $attrNode->value;
                        }
                        $output['@attributes'] = $a;
                        $output['@value'] = (string) $v;
                    }
                    else
                        $output = (string) $v;
                    // >>>>> WJ: MODIFIED CODE END >>>>>

                }
            }
            if(is_array($output)) {
                if($node->attributes->length) {
                    $a = array();
                    foreach($node->attributes as $attrName => $attrNode) {
                        $a[$attrName] = (string) $attrNode->value;
                    }
                    $output['@attributes'] = $a;
                }
                foreach ($output as $t => $v) {
                    if(is_array($v) && count($v)==1 && $t!='@attributes') {
                        $output[$t] = $v[0];
                    }
                }
            }
            break;
    }
    return $output;
}

1 个答案:

答案 0 :(得分:4)

Facebook正在嗅探用户代理,并且不会在没有提供的情况下向您提供您在浏览器中看到的XML提要。在从服务器获取XML之前,您可以通过以下几种方式处理这些问题:

ini_set("user_agent","my_awesome_magic_user_agent_which_can_be_anyhing");

或者:

stream_context_set_default(
     array(
        "http"=>array(
           "user_agent"=>"whatever"  
         )
     ));

下次,您可能希望echo您的XML字符串,以查看实际情况......