Warning: DOMDocument::load(): Extra content at the end of the document in http://widget.stagram.com/rss/n

From last two week I’m getting following php warning message

Warning: DOMDocument::load(): Extra content at the end of the document
in http://widget.stagram.com/rss/n/zee/, line: 10 in
/home//public_html/wp-content/themes//inc/social-instagram.php
on line 22

Read More

where I’m trying to parse this link in the warning message

$xmlDoc = new DOMDocument();
$xmlDoc->load($xml);

when I browse the link in web browser http://widget.stagram.com/rss/tag/zee/ the xml seems to be ok.

Related posts

Leave a Reply

2 comments

  1. You need to use curl and add the option CURLOPT_USERAGENT. That’s why it’s working on the browser, and not by simple file_get_contents or ->load. Consider this example:

    $url = ('http://widget.stagram.com/rss/tag/zee/');
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
    curl_setopt($ch, CURLOPT_FAILONERROR,1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 15);
    $data = curl_exec($ch);
    curl_close($ch);
    $xml = simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA);
    
    echo '<pre>';
    print_r($xml);
    

    Sample Output

  2. I had the same problem and found the following solution worked without the need for CURL:

    libxml_set_streams_context(
        stream_context_create(
            array(
                'http' => array(
                    'user_agent' => 'php'            
                )
            )
        )
    );
    
    $dom = new DOMDocument;
    $dom->load($xml);
    

    The extra content error went away and all the feeds I pushed through worked fine.

    Big thanks to Gordon for answering another question with this answer that led me to try it for this problem.