PHP & XML help pls

If you touch your software enough does it become hardware?

Moderator: Forum Moderators

Post Reply
ProfHawking
Zombie
Zombie
Posts: 2101
Joined: February 20th, 2005, 21:31

PHP & XML help pls

Post by ProfHawking »

It's me again, with another annoying php issue.

I'm trying to do something that should be simple, and its only half working.

I have an xml file that provides something like this:

Code: Select all

<xml>
<xml>
<rss>
  <channel>
    <title>Gold Cup Odds - Affiliate Feeds</title>
    <link>http://www.paddypower.com</link>
    <description>Gold Cup Odds</description>
    <language>en</language>

    <pubDate>Tue, 03 Mar 2009 12:15:06 GMT</pubDate>
    <dc>2009-03-03T12:15:06Z</dc>
    <dc>en</dc>
    <cf>list</cf>
    <item>
      <title>Cheltenham Gold Cup Chase 3m 2 1/2f - Denman</title>

      <link>http://www.paddypower.com/bet?action=go_racing&ev_class_id=137&ev_type_id=5599&ev_id=1072000&gid=rss&bs_add_leg_to_slip=1&leg=stamp~|price_type~L|lp_num~6|lp_den~1|hcap_value~|selections~22428326</link>
      <description>Horse Racing - CHELTENHAM - Event Time: 2009-03-13 15:20</description>
      <pubDate>Fri, 13 Mar 2009 15:20:00 GMT</pubDate>
      <guid>http://www.paddypower.com/bet?action=go_racing&ev_class_id=137&ev_type_id=5599&ev_id=1072000&gid=rss&bs_add_leg_to_slip=1&leg=stamp~|price_type~L|lp_num~6|lp_den~1|hcap_value~|selections~22428326</guid>

      <dc>2009-03-13T15:20:00Z</dc>
      <eventtype>5599</eventtype>
      <eventclass>137</eventclass>
      <event>Win or Each Way</event>
      <event>3318315</event>
      <selection>Denman</selection>

      <selection>6 - 1</selection>
      <event>1072000</event>
      <event>2009-03-13 15:20:00.0</event>
      <selection>22428326</selection>
      <event>Cheltenham Gold Cup Chase 3m 2 1/2f</event>
      <event>Cheltenham Gold Cup Chase 3m 2 1/2f</event>

      <selection>Denman</selection>
      <event>Horse Racing</event>
      <event>CHELTENHAM</event>
      <event>Horse Racing</event>
    </item>
I'm trying to use simpleXML to parse this, using code like so:

Code: Select all

function getFeed($feed_url) {
	$content = file_get_contents($feed_url);
	$x = new SimpleXmlElement($content);
	echo "<ul>";
	foreach($x->channel->item as $entry) {
		echo "<li><a href='$entry->link'>" . $entry->title . "</a></li>\n";
	}
	echo "</ul>";
}
Which works fine for the items stuff like <title> and <link>, eg: $entry->title and $entry->link
but as soon as i try to reference

Code: Select all

<selection> or <selection> 
eg: $entry->selection:title or $entry->selection:description
then it all goes tits up.

Anyone got any suggestions as to how i can do that?
ProfHawking
Zombie
Zombie
Posts: 2101
Joined: February 20th, 2005, 21:31

Post by ProfHawking »

hmm, php is cutting out my colons in <> brackets.
the xml bits are something like this: with - instead of <>
-selection:title-Denman-/selection:title-
-selection:description-Denman-/selection:description-
Stoat
Site Admin
Site Admin
Posts: 3291
Joined: October 8th, 2004, 15:48
Location: Sheffield, UK
Contact:

Post by Stoat »

I'm not well up on php's SimpleXML. Something to do with namespaces?
http://uk.php.net/manual/en/function.si ... spaces.php
ProfHawking
Zombie
Zombie
Posts: 2101
Joined: February 20th, 2005, 21:31

Post by ProfHawking »

i think you are right, something to do with namespaces
but i cant get my head around that example on php.net

This guy has the same issue i think:
http://www.experts-exchange.com/Web_Dev ... 41096.html

I wish there was an easy to understand example on the end of that though.
Stoat
Site Admin
Site Admin
Posts: 3291
Joined: October 8th, 2004, 15:48
Location: Sheffield, UK
Contact:

Post by Stoat »

This worked for me on your feed:
http://blog.sherifmansour.com/?p=302

Code: Select all

$namespaces = $x->getNameSpaces(true);
--and--
$event = $entry->children($namespaces['event']);

Code: Select all

function getFeed($feed_url) {
	$content = file_get_contents($feed_url);
	$x = new SimpleXmlElement($content);
	echo "<ul>";
	$namespaces = $x->getNameSpaces(true);
	foreach($x->channel->item as $entry) {
		$event = $entry->children($namespaces['event']);
		echo "<li><a href='$entry->link'>" . $entry->title . "</a> Type: " . $event->type .  "</li>\n";
	}
	echo "</ul>";
}
ProfHawking
Zombie
Zombie
Posts: 2101
Joined: February 20th, 2005, 21:31

Post by ProfHawking »

Brilliantosos!

got it working using this sort of thing:

Code: Select all

	foreach($x->channel->item as $entry) {
		
		$link = $entry->link."&AFF_ID=10000005";
		$horse = $entry->children("http://rss.paddypower.com/rss/rdf-paddypower/selection")->title;
		$odds = $entry->children("http://rss.paddypower.com/rss/rdf-paddypower/selection")->price;
Thanks CodeNinja! :ninja:
Post Reply