0

I'm trying to parse an RSS feed in PHP for the first time. It seems to go fine until I actually try to display anything! This example is me trying to pull out four random organization names from the feed (I actually want to display more, but am keeping it simple here...)

$xml = file_get_contents('https://rss.myinterfase.com/rss/oxford_RSS_Jobs_xml.xml');
foreach($xml->Row as $job) {
$item[] = array(
                 'OrganizationName'  => (string)$job->OrganizationName,
                 'job_JobTitle'      => (string)$job->job_JobTitle,
                 'job_expiredate'    =>  strtotime($job->job_expiredate),
                 'ExternalLink'      => $job->ExternalLink
                 );
}
$rand_job = array_rand($item, 4);
$i=0;
echo '<ul>';
while($i<=3) { 
echo '<li>';
echo $item[$i]['OrganizationName'];
echo '</li>';
$i++;
}
echo '</ul>'

What do I need to do differently? Thanks!

JohnG
  • 486
  • 3
  • 22
  • `$xml` will contain only a string. You cannot do `foreach` on `$xml->Row`. Use, for example, [DOMDocument](http://php.net/manual/en/class.domdocument.php) instead. Try [this](http://stackoverflow.com/questions/14659162/loading-rss-file-with-domdocument), [this](http://stackoverflow.com/questions/4336491/rss-parsing-domdocument-in-php) or [this](http://stackoverflow.com/questions/18906366/parsing-an-rss-feed-in-php-with-dom). – machineaddict Apr 28 '15 at 12:27
  • 1
    There are a number of ways to do this but as @machineaddict has said that $xml is just a string until you make it an object. Here are a list of tools you can use to make it an object and parse it, http://php.net/manual/en/refs.xml.php. I usually use the SimpleXML. – chris85 Apr 28 '15 at 12:30
  • Awesome - thank you. I've used SimpleXML and it works - used http://blog.stuartherbert.com/php/2007/01/07/using-simplexml-to-parse-rss-feeds/ for a bit of a guideline. – JohnG Apr 28 '15 at 12:44

1 Answers1

0

You have to use simplexml_load_file($url); or similar.

$url = 'https://rss.myinterfase.com/rss/oxford_RSS_Jobs_xml.xml';

$xml = simplexml_load_file($url);
foreach($xml->row as $job) { // be sure about $xml->row. If it's full path to this elements
//..... your code
}
Kristiyan
  • 1,655
  • 14
  • 17