SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Apr 2002
    Posts
    395
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:

    PHP Code:
        $rss = new DOMDocument('1.0''iso-8859-1');
        
    $rss->load('load_content.php');
        
    $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/domain.com/htdocs/load_content.php, line: 1 in /home/admin/sites/domain.com/htdocs/feed.php on line 8
    Notice: Undefined offset: 0 in /home/admin/sites/domain.com/htdocs/feed.php 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,

    $rss->load('file.xml');

    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?

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,139
    Mentioned
    190 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by IamAdam View Post
    .....
    it looks like an xml file when i view it on the browser.
    .....
    But is it? What HTTP headers for type is it returning?

  3. #3
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,258
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    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.
    "First make it work. Then make it better."

  4. #4
    SitePoint Addict
    Join Date
    Apr 2002
    Posts
    395
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mittineague View Post
    But is it? What HTTP headers for type is it returning?
    Yes, load_content.php has header('Content-type: application/xml'); in the first line.



    Quote Originally Posted by Jeff Mott View Post
    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.
    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?

  5. #5
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    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, http://myserver.com/path/to/load_content.php

    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?
    Salathe
    Software Developer and PHP Manual Author.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •