Hi there everyone!

My issue: I'm having a problem finding a way to import current ebay auctions into a website database, so I thought I'd try to scrape the information from an RSS feed of current listings. I subscribed to a third party feed system and learned a bit about simplexml_load_string. I found a function in the comments that was supposed to allow for a more graceful success/failure of the feed load and I tried to plug my info into it.

My feed: http://bayfeeds.com/wheeltasticsales.xml

<?php
function loadXML2($domain, $path, $timeout = 30) { 

/* 
    Usage: 

    $xml = loadXML2("bayfeeds.com", "/wheeltasticsales.xml"); 
    if($xml) { 
        // xml doc loaded 
    } else { 
        // failed. show friendly error message. 
    } 
*/ 

$fp = fsockopen($domain, 80, $errno, $errstr, $timeout); 
if($fp) { 
    // make request 
    $out = "GET $path HTTP/1.1\r\n"; 
    $out .= "Host: $domain\r\n"; 
    $out .= "Connection: Close\r\n\r\n"; 
    fwrite($fp, $out); 

    // get response 
    $resp = ""; 
    while (!feof($fp)) { 
        $resp .= fgets($fp, 128); 
    } 
    fclose($fp); 
    // check status is 200 
    $status_regex = "/HTTP\/1\.\d\s(\d+)/"; 
    if(preg_match($status_regex, $resp, $matches) && $matches[1] == 200) {    
        // load xml as object 
        $parts = explode("\r\n\r\n", $resp);    
        return simplexml_load_string($parts[1]);                
    } 
} 
return false; 

}

$xml = loadXML2("bayfeeds.com", "/wheeltasticsales.xml"); 
if($xml) { 
	// xml doc loaded 
	print_r($xml);
} else { 
	// failed. show friendly error message. 
	echo'XML load failed.';
}

?>

But I got some errors that I don't understand at all:

Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 1: parser error : Start tag expected, '<' not found in /home/wheeltastic/public_html/rss.php on line 34

Warning: simplexml_load_string() [function.simplexml-load-string]: 145a in /home/wheeltastic/public_html/rss.php on line 34

Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in /home/wheeltastic/public_html/rss.php on line 34
XML load failed.

Line 34:

return simplexml_load_string($parts[1]); 

Could someone help me figure out what I'm doing wrong?

Thanks for your time!

    Have you checked that what you're receiving is valid? The error messages are saying there's something at the start of the content that shouldn't be there.

    Wouldn't [man]simplexml_load_file[/man] be much more straightforward (note the tip on that page wrt errors)?

      Thanks very much for your help!

      Last night while googling, I found a second way to skin my cat, so I gave it a shot and got a good return.

      function getFeed($feed_url) {
      
      $content = file_get_contents($feed_url);
      $x = new SimpleXmlElement($content);
      
      foreach($x->channel->item as $entry) {
      
      	$parts = parse_url($entry->link);
      	parse_str($parts['query'], $query);
      	$itemnum = $query['i'];
      
      	echo "
          <h3><a href='$entry->link' title='$entry->title'>" . $entry->title . "</a></h3>
          item number: ".$itemnum."<br>
          Price: ".$entry->BuyItNowPrice."<br>
          <br>
          ";
      }
      echo "</ul>";
      }
      
      getFeed("http://bayfeeds.com/wheeltasticsales.xml"); 

      This script reads the file and it returns elements. My problem now lies in my ability to get additional elements, due to their name.

      When you look at the XML file, there title and link work fine with the script, but then I have an element :

      <rx:BuyItNowPrice xmlns:rx="urn:ebay:apis:eBLBaseComponents">15700</rx:BuyItNowPrice>

      Which I am unable to retrieve. I've tried "rx:BuyItNowPrice" and "BuyItNowPrice", but both are ineffective.

      How would I grab this element from the xml file?

        You've discovered XML namespaces. You can specify a namespace when asking for an element's children, but I don't know how they're aliased; maybe they aren't.

          Write a Reply...