Hey Having a bit of trouble with SimpleXML, I probably should scan few the manual but I've looked at the first bit of code on their and I can't see why my code doesn't act the way I think it should.. This is what I have: <?php $xml = "http://ipod.break.com/channel/homepage/index.php"; $xml = new SimpleXMLElement($xml, null, true); echo $xml->item[0]->title; ?> PHP: As you can see, I'm trying to grab the title, basically what I want to do is end up with a script that when run scans the rss feed and adds things to the database (title & description & file url) and if they are already in the db, it skips/ends the script. Anyway, I thought the code above would show the title of the first item in the rss feed, but it seems not. Any ideas why?
Try <?php $xml = "http://ipod.break.com/channel/homepage/index.php"; $xml_doc = new SimpleXMLElement($xml, null, true); echo $xml_doc->item[0]->title; ?> PHP: This is just in the case you were overwriting the $xml variable when using it again in second line... If that is not the problem, try debugging the $xml_doc object by using print_r($xml_doc):
That still didn't work so I used the print_r thing and got all this: SimpleXMLElement Object ( [@attributes] => Array ( [version] => 2.0 ) [channel] => SimpleXMLElement Object ( [title] => BREAK.com - [link] => http://www.break.com/ [language] => en-us [copyright] => BREAK.com (c) 1998 - 2007 [pubDate] => Mon, 31 Dec 2007 08:19:09 GMT [lastBuildDate] => Mon, 31 Dec 2007 08:19:09 GMT [ttl] => 60 [item] => Array ( [0] => SimpleXMLElement Object ( [title] => Firefighter Chief Snake Prank [link] => http://www.Break.com/index/firefighter-scare-prank.html [description] => A bunch of guys at a firehouse pull a prank on their boss with a fake snake. His reaction to the snake is hilarious. [comments] => http://my.break.com/Content/Comment/ContentComment.aspx?ContentID=e%2fxUhItUypkd%2bFy30pqjuw%3d%3d [pubDate] => Mon, 31 Dec 2007 05:31:12 GMT [enclosure] => SimpleXMLElement Object ( [@attributes] => Array ( [type] => video/mp4 [url] => http://media1.break.com/dnet/media/2007/12/firefighter-scare-prank.mp4 [length] => 10000 ) ) [guid] => http://www.Break.com/index/firefighter-scare-prank.html ) [1] => SimpleXMLElement Object ( [title] => Chick Faceplants After Nasty Spike [link] => http://www.Break.com/index/chick-faceplants-after-nasty-spike.html [description] => A chick takes a spike to the head that was so hard it knocked her off her feet and face first onto the gym floor. [comments] => http://my.break.com/Content/Comment/ContentComment.aspx?ContentID=WC1w0PJVLenLMn09Vt7m8w%3d%3d [pubDate] => Mon, 31 Dec 2007 05:18:51 GMT [enclosure] => SimpleXMLElement Object ( [@attributes] => Array ( [type] => video/mp4 [url] => http://media1.break.com/dnet/media/2007/12/chick-faceplants-after-nasty-spike.mp4 [length] => 10000 ) ) [guid] => http://www.Break.com/index/chick-faceplants-after-nasty-spike.html ) [2] => SimpleXMLElement Object ( [title] => Angry Grandpa Finds Out Hes On Break [link] => http://www.Break.com/index/angry-grandpa-finds-out-hes-on-break.html [description] => At first I really didnt like The Angry Grandpa, but now Im starting to think hes hilarious. I could see the Angry Grandpa becoming a regular thing. [comments] => http://my.break.com/Content/Comment/ContentComment.aspx?ContentID=MkFApB1klhHas0XnpsRI7g%3d%3d [pubDate] => Sun, 30 Dec 2007 18:07:25 GMT [enclosure] => SimpleXMLElement Object ( [@attributes] => Array ( [type] => video/mp4 [url] => http://media1.break.com/dnet/media/2007/12/angry-grandpa-finds-out-hes-on-break.mp4 [length] => 10000 ) ) [guid] => http://www.Break.com/index/angry-grandpa-finds-out-hes-on-break.html ) [3] => SimpleXMLElement Object ( [title] => Teeth Trailer [link] => http://www.Break.com/index/teeth-trailer.html [description] => Worried about getting an STD from that one night stand you had? Thats nothing compared to a vagina that has teeth. [comments] => http://my.break.com/Content/Comment/ContentComment.aspx?ContentID=piiJJ%2fjKuJd%2fTs%2bQKtVu7w%3d%3d [pubDate] => Sun, 30 Dec 2007 18:02:30 GMT [enclosure] => SimpleXMLElement Object ( [@attributes] => Array ( [type] => video/mp4 [url] => http://media1.break.com/dnet/media/2007/12/teeth-trailer.mp4 [length] => 10000 ) ) [guid] => http://www.Break.com/index/teeth-trailer.html ) [4] => SimpleXMLElement Object ( [title] => Amazing Paper Airplane Flight Over NYC [link] => http://www.Break.com/index/amazing-paper-airplane-flight-over-nyc.html [description] => This guy launches a paper airplane off the 30th floor of a building in the financial district of New York City. The flight it goes on without crashing is pretty amazing. Must be some kind of record. [comments] => http://my.break.com/Content/Comment/ContentComment.aspx?ContentID=67qUR9TKPQwb9ZtSaeDdyw%3d%3d [pubDate] => Sun, 30 Dec 2007 18:00:32 GMT [enclosure] => SimpleXMLElement Object ( [@attributes] => Array ( [type] => video/mp4 [url] => http://media1.break.com/dnet/media/2007/12/amazing-paper-airplane-flight-over-nyc.mp4 [length] => 10000 ) ) [guid] => http://www.Break.com/index/amazing-paper-airplane-flight-over-nyc.html ) [5] => SimpleXMLElement Object ( [title] => Top 10 Internet Videos of 2007 [link] => http://www.Break.com/index/top-10-internet-videos-of-2007.html [description] => A lot of people have been asking so we finally sat down and came up with our list of the top 10 internet videos of 2007. Enjoy and have a safe New Years Eve! [comments] => http://my.break.com/Content/Comment/ContentComment.aspx?ContentID=p7%2fbyX2usAkH3FuGyyX8mA%3d%3d [pubDate] => Sun, 30 Dec 2007 17:55:06 GMT [enclosure] => SimpleXMLElement Object ( [@attributes] => Array ( [type] => video/mp4 [url] => http://media1.break.com/dnet/media/2007/12/top-10-internet-videos-of-2007.mp4 [length] => 10000 ) ) [guid] => http://www.Break.com/index/top-10-internet-videos-of-2007.html ) [6] => SimpleXMLElement Object ( [title] => Fastest Way To Open A Champagne Bottle [link] => http://www.Break.com/index/fastest-way-to-open-a-champagne-bottle.html [description] => With New Years Day less than one day away this guy wanted to demonstrate the fastest way to open up a champagne bottle. I have a feeling this is something you likely dont get right on your first attempt. [comments] => http://my.break.com/Content/Comment/ContentComment.aspx?ContentID=4XA4yRA0KrJttGhtI%2fxeMw%3d%3d [pubDate] => Sun, 30 Dec 2007 03:43:40 GMT [enclosure] => SimpleXMLElement Object ( [@attributes] => Array ( [type] => video/mp4 [url] => http://media1.break.com/dnet/media/2007/12/fastest-way-to-open-a-champagne-bottle.mp4 [length] => 10000 ) ) [guid] => http://www.Break.com/index/fastest-way-to-open-a-champagne-bottle.html ) [7] => SimpleXMLElement Object ( [title] => Iraqi Interrogation [link] => http://www.Break.com/index/iraqi-interrogation.html [description] => Always good to see the guys in Iraq having a good time. I got a kick out of this one. [comments] => http://my.break.com/Content/Comment/ContentComment.aspx?ContentID=lVkSDm18X2KTKiyDdjkS5w%3d%3d [pubDate] => Sun, 30 Dec 2007 03:28:09 GMT [enclosure] => SimpleXMLElement Object ( [@attributes] => Array ( [type] => video/mp4 [url] => http://media1.break.com/dnet/media/2007/12/iraqi-interrogation.mp4 [length] => 10000 ) ) [guid] => http://www.Break.com/index/iraqi-interrogation.html ) [8] => SimpleXMLElement Object ( [title] => Skinny Girl Owned By Desert Eagle [link] => http://www.Break.com/index/skinny-girl-owned-by-desert-eagle.html [description] => A skinny girl in a bikini tries firing a .50 Desert Eagle and gets a pretty good kick back. Maybe if she weighed more than 75 pounds then this might not have happened. [comments] => http://my.break.com/Content/Comment/ContentComment.aspx?ContentID=nU7CTUOIc1fIw%2fLijb1nIw%3d%3d [pubDate] => Sat, 29 Dec 2007 19:53:09 GMT [enclosure] => SimpleXMLElement Object ( [@attributes] => Array ( [type] => video/mp4 [url] => http://media1.break.com/dnet/media/2007/12/skinny-girl-owned-by-desert-eagle.mp4 [length] => 10000 ) ) [guid] => http://www.Break.com/index/skinny-girl-owned-by-desert-eagle.html ) [9] => SimpleXMLElement Object ( [title] => Cheerleader Taken Out Hard [link] => http://www.Break.com/index/cheerleader-taken-out-hard.html [description] => Some cheerleader gets taken out hard by her teammate at some national championship. This was for the large seniors so you bet this hurt like hell. [comments] => http://my.break.com/Content/Comment/ContentComment.aspx?ContentID=xIgSFY8BPYmDzzucOEOyBw%3d%3d [pubDate] => Sat, 29 Dec 2007 09:12:06 GMT [enclosure] => SimpleXMLElement Object ( [@attributes] => Array ( [type] => video/mp4 [url] => http://media1.break.com/dnet/media/2007/12/cheerleader-taken-out-hard.mp4 [length] => 10000 ) ) [guid] => http://www.Break.com/index/cheerleader-taken-out-hard.html ) [10] => SimpleXMLElement Object ( [title] => Dumb Blonde Interviews John Cusak [link] => http://www.Break.com/index/dumb-blonde-interviews-john-cusak.html [description] => From what I can make of this some dumb blonde sits down to interview John Cusak and confuses him for Kevin Spacey. What a moron. [comments] => http://my.break.com/Content/Comment/ContentComment.aspx?ContentID=rOGA6o%2bn7GjcOIOX5rUBRg%3d%3d [pubDate] => Sat, 29 Dec 2007 09:05:08 GMT [enclosure] => SimpleXMLElement Object ( [@attributes] => Array ( [type] => video/mp4 [url] => http://media1.break.com/dnet/media/2007/12/dumb-blonde-interviews-john-cusak.mp4 [length] => 10000 ) ) [guid] => http://www.Break.com/index/dumb-blonde-interviews-john-cusak.html ) [11] => SimpleXMLElement Object ( [title] => Stupid Theif Busts Head On Wrong Window [link] => http://www.Break.com/index/stupid-theif-busts-head-on-wrong-door.html [description] => One of the dumbest criminals of the year. Watch as he breaks a window, quickly comes in the building, steals a five dollar tip jar, then runs into the other window. [comments] => http://my.break.com/Content/Comment/ContentComment.aspx?ContentID=JqXJ%2b6y3arWJ3SBb%2bY0Ufg%3d%3d [pubDate] => Fri, 28 Dec 2007 23:37:37 GMT [enclosure] => SimpleXMLElement Object ( [@attributes] => Array ( [type] => video/mp4 [url] => http://media1.break.com/dnet/media/2007/12/stupid-theif-busts-head-on-wrong-door.mp4 [length] => 10000 ) ) [guid] => http://www.Break.com/index/stupid-theif-busts-head-on-wrong-door.html ) [12] => SimpleXMLElement Object ( [title] => The Greatest Prank Of All Time [link] => http://www.Break.com/index/the-greatest-prank-ever.html [description] => This might be one of the greatest pranks of all time. This dude gets so scared near the end I thought he was going to cry. [comments] => http://my.break.com/Content/Comment/ContentComment.aspx?ContentID=YcNBi0jwQTdDi1Y1RhpsXg%3d%3d [pubDate] => Fri, 28 Dec 2007 23:32:24 GMT [enclosure] => SimpleXMLElement Object ( [@attributes] => Array ( [type] => video/mp4 [url] => http://media1.break.com/dnet/media/2007/12/the-greatest-prank-ever.mp4 [length] => 10000 ) ) [guid] => http://www.Break.com/index/the-greatest-prank-ever.html ) [13] => SimpleXMLElement Object ( [title] => The Dancing Weather Girl [link] => http://www.Break.com/index/the-dancing-weather-girl.html [description] => CW11 weather girl girl Jill Nicolini busts out in the Soulja Boy dance and gets a couple others to join her while on the air. Damn she is hot! [comments] => http://my.break.com/Content/Comment/ContentComment.aspx?ContentID=jt6RmbwmiWJ%2bC1uAn3OEJg%3d%3d [pubDate] => Fri, 28 Dec 2007 20:52:34 GMT [enclosure] => SimpleXMLElement Object ( [@attributes] => Array ( [type] => video/mp4 [url] => http://media1.break.com/dnet/media/2007/12/the-dancing-weather-girl.mp4 [length] => 10000 ) ) [guid] => http://www.Break.com/index/the-dancing-weather-girl.html ) [14] => SimpleXMLElement Object ( [title] => Hang Glider Faceplants On Takeoff [link] => http://www.Break.com/index/hanglider-faceplants-after-takeoff.html [description] => This guy runs full speed down a steep mountain with a prototype hang glider but still doesnt have enough speed for the launch so he lands face first into the ground. [comments] => http://my.break.com/Content/Comment/ContentComment.aspx?ContentID=s3%2fgAKMm7RyXGnDUrm75lw%3d%3d [pubDate] => Fri, 28 Dec 2007 20:13:38 GMT [enclosure] => SimpleXMLElement Object ( [@attributes] => Array ( [type] => video/mp4 [url] => http://media1.break.com/dnet/media/2007/12/hanglider-faceplants-after-takeoff.mp4 [length] => 10000 ) ) [guid] => http://www.Break.com/index/hanglider-faceplants-after-takeoff.html ) [15] => SimpleXMLElement Object ( [title] => Backyard Skunk Catapult [link] => http://www.Break.com/index/backyard-skunk-catapult.html [description] => These guys were sick of skunks going through their garbage so they invented a sweet skunk catapult. That will teach them, no skunks were seriously hurt. [comments] => http://my.break.com/Content/Comment/ContentComment.aspx?ContentID=dovDocQtf1hR4n6Swb0MPQ%3d%3d [pubDate] => Fri, 28 Dec 2007 19:02:22 GMT [enclosure] => SimpleXMLElement Object ( [@attributes] => Array ( [type] => video/mp4 [url] => http://media1.break.com/dnet/media/2007/12/backyard-skunk-catapult.mp4 [length] => 10000 ) ) [guid] => http://www.Break.com/index/backyard-skunk-catapult.html ) [16] => SimpleXMLElement Object ( [title] => David Blaine Street Magic 3 [link] => http://www.Break.com/index/david-blaine-street-magic-3.html [description] => David Blaine sets his sights on the same two effeminate idiots from the last two videos. And unfortunately for them, the third time's the charm. [comments] => http://my.break.com/Content/Comment/ContentComment.aspx?ContentID=lZz83aXpmz0Dhs0SKNKPig%3d%3d [pubDate] => Fri, 28 Dec 2007 18:23:29 GMT [enclosure] => SimpleXMLElement Object ( [@attributes] => Array ( [type] => video/mp4 [url] => http://media1.break.com/dnet/media/2007/12/david-blaine-street-magic-3.mp4 [length] => 10000 ) ) [guid] => http://www.Break.com/index/david-blaine-street-magic-3.html ) [17] => SimpleXMLElement Object ( [title] => Another Failed Backflip [link] => http://www.Break.com/index/another-failed-backflip2.html [description] => This kid tries to do a backflip in his kitchen but ended up faceplanting and knocking out one of his teeth. [comments] => http://my.break.com/Content/Comment/ContentComment.aspx?ContentID=E6b0Dh47N1cnNrErjnFdPw%3d%3d [pubDate] => Fri, 28 Dec 2007 04:12:07 GMT [enclosure] => SimpleXMLElement Object ( [@attributes] => Array ( [type] => video/mp4 [url] => http://media1.break.com/dnet/media/2007/12/another-failed-backflip2.mp4 [length] => 10000 ) ) [guid] => http://www.Break.com/index/another-failed-backflip2.html ) [18] => SimpleXMLElement Object ( [title] => The Next Tony Hawk [link] => http://www.Break.com/index/the-next-tony-hawk.html [description] => This kid is barely one years old and although he nearly took out the laptop he never falls. This might be the first skateboard video I posted that didnt end in a wipeout. [comments] => http://my.break.com/Content/Comment/ContentComment.aspx?ContentID=9%2fLO1Vn1MA8q%2b3bYXvNd7g%3d%3d [pubDate] => Fri, 28 Dec 2007 04:10:08 GMT [enclosure] => SimpleXMLElement Object ( [@attributes] => Array ( [type] => video/mp4 [url] => http://media1.break.com/dnet/media/2007/12/the-next-tony-hawk.mp4 [length] => 10000 ) ) [guid] => http://www.Break.com/index/the-next-tony-hawk.html ) [19] => SimpleXMLElement Object ( [title] => Brother Blow Dryer Prank Ownage [link] => http://www.Break.com/index/brother-blow-dryer-prank-ownage.html [description] => Some guy gets his brother, who happens to be a professional body builder, pretty good with the old flour in the blow dryer prank. Careful dude hes gonna kick your butt! [comments] => http://my.break.com/Content/Comment/ContentComment.aspx?ContentID=5Y942FzDgE3qoQhejcfp0Q%3d%3d [pubDate] => Fri, 28 Dec 2007 01:09:33 GMT [enclosure] => SimpleXMLElement Object ( [@attributes] => Array ( [type] => video/mp4 [url] => http://media1.break.com/dnet/media/2007/12/brother-blow-dryer-prank-ownage.mp4 [length] => 10000 ) ) [guid] => http://www.Break.com/index/brother-blow-dryer-prank-ownage.html ) [20] => SimpleXMLElement Object ( [title] => Ant Eater Cant Get it Up [link] => http://www.Break.com/index/ant-eater-cant-get-it-up.html [description] => This Ant Eaters manhood has a mind of its own. It just cant decide whether or not to get up! Poor guy needs some Cialis. [comments] => http://my.break.com/Content/Comment/ContentComment.aspx?ContentID=J%2bhbq%2bfYKUuub1xZ7f5Irw%3d%3d [pubDate] => Fri, 28 Dec 2007 01:05:24 GMT [enclosure] => SimpleXMLElement Object ( [@attributes] => Array ( [type] => video/mp4 [url] => http://media1.break.com/dnet/media/2007/12/ant-eater-cant-get-it-up.mp4 [length] => 10000 ) ) [guid] => http://www.Break.com/index/ant-eater-cant-get-it-up.html ) [21] => SimpleXMLElement Object ( [title] => Downhill Toy Tractor Faceplant [link] => http://www.Break.com/index/downhill-toy-tractor-faceplant.html [description] => This looks like a really fun game, jump on your toy tractor and ride it as fast as you can downhill while your buddy films. [comments] => http://my.break.com/Content/Comment/ContentComment.aspx?ContentID=aQd1YAJO4ER9ZfCo9ycSsQ%3d%3d [pubDate] => Thu, 27 Dec 2007 19:46:04 GMT [enclosure] => SimpleXMLElement Object ( [@attributes] => Array ( [type] => video/mp4 [url] => http://media1.break.com/dnet/media/2007/12/downhill-toy-tractor-faceplant.mp4 [length] => 10000 ) ) [guid] => http://www.Break.com/index/downhill-toy-tractor-faceplant.html ) [22] => SimpleXMLElement Object ( [title] => Airplane Knocks Over Truck [link] => http://www.Break.com/index/airplane-knocks-over-truck.html [description] => Shortly after landing this South African pilot knocks over a large box truck with the left wing. [comments] => http://my.break.com/Content/Comment/ContentComment.aspx?ContentID=W7JMHCaa0Ix%2fIvJoxJV7Og%3d%3d [pubDate] => Thu, 27 Dec 2007 19:40:27 GMT [enclosure] => SimpleXMLElement Object ( [@attributes] => Array ( [type] => video/mp4 [url] => http://media1.break.com/dnet/media/2007/12/airplane-knocks-over-truck.mp4 [length] => 10000 ) ) [guid] => http://www.Break.com/index/airplane-knocks-over-truck.html ) [23] => SimpleXMLElement Object ( [title] => Chick Plows Into Gas Station [link] => http://www.Break.com/index/chick-plows-into-gas-station.html [description] => Early in the morning a chick goes flying full speed into a gas pump. Somehow nobody was seriously hurt in this accident. [comments] => http://my.break.com/Content/Comment/ContentComment.aspx?ContentID=YRHaL09RI71UETpsLVF%2bng%3d%3d [pubDate] => Thu, 27 Dec 2007 18:47:33 GMT [enclosure] => SimpleXMLElement Object ( [@attributes] => Array ( [type] => video/mp4 [url] => http://media1.break.com/dnet/media/2007/12/chick-plows-into-gas-station.mp4 [length] => 10000 ) ) [guid] => http://www.Break.com/index/chick-plows-into-gas-station.html ) [24] => SimpleXMLElement Object ( [title] => Name This Song [link] => http://www.Break.com/index/name-this-song.html [description] => This is a very impressive video. Try and name the song this guy is singing before the video is half over. [comments] => http://my.break.com/Content/Comment/ContentComment.aspx?ContentID=BEy1cY0aMrq6C1Fp24SzuQ%3d%3d [pubDate] => Thu, 27 Dec 2007 17:39:48 GMT [enclosure] => SimpleXMLElement Object ( [@attributes] => Array ( [type] => video/mp4 [url] => http://media1.break.com/dnet/media/2007/12/name-this-song.mp4 [length] Code (markup): (was too long so shortened it)
In that case, i guess you could access the title of the first item element like this: echo $xml_doc->channel->item[0]->title; PHP: This would be the code for loop to access the titles of all items: foreach($xml_doc->channel->item as $itm) { echo $itm->title . "<br />"; } PHP: