DOMDocument load PHP file?

Hi guys,

I am using a load_content.php file to grab an external rss feed, and the output of that file is a xml format (Content-type: text/xml) and it looks like an xml file when i view it on the browser.

Then I have feed.php on which I try to do this:

    $rss = new DOMDocument('1.0', 'iso-8859-1');
    $feed = array();
    foreach ($rss->getElementsByTagName('item') as $node) {
    $item = array (
    'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
    'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
    'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
    'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
    array_push($feed, $item);
    $limit = 8;
    for($x=0;$x<$limit;$x++) {
    $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
    $link = $feed[$x]['link'];
    $description = $feed[$x]['desc'];
    $date = date('l F d, Y', strtotime($feed[$x]['date']));
    echo '<p><strong><a href="'.$link.'" title="'.$title.'">'.$title.'</a></strong><br />';
    echo '<small><em>Posted on '.$date.'</em></small></p>';
    echo '<p>'.$description.'</p>';

I always get an error
Warning: DOMDocument::load() [domdocument.load]: xmlParsePI : no target name in /home/admin/sites/, line: 1 in /home/admin/sites/ on line 8
Notice: Undefined offset: 0 in /home/admin/sites/ on line 21

The thing is, if i create the xml file manually and not retrieve the data from the php file, and I do this,


it works flawlessly. But it doesn’t help because I need to grab the rss feed using php at load_content.php

Anyone could help?

But is it? What HTTP headers for type is it returning?

Also, the DOMDocument load method isn’t sending an HTTP request and parsing an HTTP response, like the browser would do. It’s reading the file straight off the disk, which means it’s reading the raw PHP source.

Yes, load_content.php has header(‘Content-type: application/xml’); in the first line.

I think this is the problem… do you know if there is any way to fix this? IE. have an alternative to DOMDocument load via HTTP request?

It looks like your load_content.php is intended to be served through a web server. Use a URL instead of a file path; for example,

That said, it is probably not a good idea to be calling your load_content.php script from within feed.php every time. Why not save the generated XML response from load_content.php into an XML file somewhere then load that with feed.php?