Jump to content


Photo
- - - - -

php rss to array function


  • Please log in to reply
15 replies to this topic

#1 curthard89

curthard89

    Young Padawan

  • Members
  • Pip
  • 226 posts

Posted 21 July 2007 - 03:02 PM

this isnt really a tutorial, but where else do i put it?

anyway, i was just playing around with rss things, and made this

i know people find rss parsing difficult......so...here u go

<?php
	
	$rss_tags = array(
		'title',
		'link',
		'guid',
		'comments',
		'description',
		'pubDate',
		'category',
	);
	$rss_item_tag = 'item';
	$rss_url = 'http://www.pixel2life.com/feeds/adobe_fireworks.xml';
	
	$rssfeed = rss_to_array($rss_item_tag,$rss_tags,$rss_url);
	
	echo '<pre>';
	print_r($rssfeed);

	function rss_to_array($tag, $array, $url) {
		$doc = new DOMdocument();
		$doc->load($url);
		$rss_array = array();
		$items = array();
		foreach($doc->getElementsByTagName($tag) AS $node) {	
			foreach($array AS $key => $value) {
				$items[$value] = $node->getElementsByTagName($value)->item(0)->nodeValue;
			}
			array_push($rss_array, $items);
		}
		return $rss_array;
	}
?>

just set up what $rss_item_tag is - the is p2l case it is 'item'

then set up the array of tags inside that

then the url to the feed

and it will give u back an array of the feed so u can do wat u please with.

comments would be appreciated, cheers

Edited by curthard89, 21 July 2007 - 04:05 PM.


#2 Corey

Corey

    Young Padawan

  • Members
  • Pip
  • 12 posts
  • Gender:Male
  • Location:Canada

Posted 29 July 2007 - 12:03 PM

Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /home/user/domain/rss.php on line 27

Line 27: $items[$value] = $node->getElementsByTagName($value)->item(0)->nodeValue;

(Using as default retrieving rss from p2l.

#3 curthard89

curthard89

    Young Padawan

  • Members
  • Pip
  • 226 posts

Posted 29 July 2007 - 04:11 PM

i copied exactly what i put and just tried it...works fine...lol, ive attached an image....and wala...

Attached Thumbnails

  • Picture_1.png

Edited by curthard89, 29 July 2007 - 04:23 PM.


#4 Corey

Corey

    Young Padawan

  • Members
  • Pip
  • 12 posts
  • Gender:Male
  • Location:Canada

Posted 29 July 2007 - 10:45 PM

i copied exactly what i put and just tried it...works fine...lol, ive attached an image....and wala...


Are you using php4 or 5?

I am running on 4.

#5 curthard89

curthard89

    Young Padawan

  • Members
  • Pip
  • 226 posts

Posted 30 July 2007 - 02:19 AM

running 5, tried at work on our server which runs 4 and works fine aswell hmmm

#6 Corey

Corey

    Young Padawan

  • Members
  • Pip
  • 12 posts
  • Gender:Male
  • Location:Canada

Posted 30 July 2007 - 12:39 PM

That is really weird then. hmm

#7 101key

101key

    Young Padawan

  • Members
  • Pip
  • 1 posts

Posted 05 September 2008 - 09:14 PM

nice

#8 rc69

rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 06 September 2008 - 01:25 PM

The DOMdocument class is a PHP5 class that is not available in PHP4 (ref: http://php.net/manua....construct.php).

Object iteration is also only available in PHP5 (ref: http://php.net/manua...iterations.php).

So technically speaking, the error Corey got should have been at the following line:
foreach($doc->getElementsByTagName($tag) AS $node) {
But since it wasn't, i can only assume PHP4 assumed something that allowed it to continue, improperly setting $node as something that wasn't an object.

Without testing this out my self, i can only say the above is an educated guess as to what happened.
Note for those still running PHP4 though, their are other XML handling functions out there :lol:

#9 UnrealMedia

UnrealMedia

    Young Padawan

  • Members
  • Pip
  • 72 posts

Posted 16 September 2008 - 04:14 PM

Its not parsing...unless this is how its suposed to be...

Array
(
	[0] => Array
		(
			[title] => Creating Fading Light Vectors in Fireworks
			[link] => http://www.pixel2life.com/tutorials/count/61837/creating_fading_light_vectors_in_fireworks/
			[guid] => http://www.pixel2life.com/tutorials/count/61837/creating_fading_light_vectors_in_fireworks/
			[comments] => http://www.pixel2life.com/comments/61837/creating_fading_light_vectors_in_fireworks/
			[description] =>  Creating Fading Light Vectors in Fireworks
			[pubDate] => Thu, 10 Jul 2008 16:00 EDT
			[category] => Fireworks Tutorials
		)

	[1] => Array
		(
			[title] => Create a Vibrant Digital Collage Mixing Buildings and Vector Shapes on Paper
			[link] => http://www.pixel2life.com/tutorials/count/61836/create_a_vibrant_digital_collage_mixing_buildings_and_vector_shapes_on_paper/
			[guid] => http://www.pixel2life.com/tutorials/count/61836/create_a_vibrant_digital_collage_mixing_buildings_and_vector_shapes_on_paper/
			[comments] => http://www.pixel2life.com/comments/61836/create_a_vibrant_digital_collage_mixing_buildings_and_vector_shapes_on_paper/
			[description] =>  Create a Vibrant Digital Collage Mixing Buildings and Vector Shapes on Paper
			[pubDate] => Thu, 10 Jul 2008 15:30 EDT
			[category] => Fireworks Tutorials
		)

	[2] => Array
		(
			[title] => Introduction to Tooltips in Fireworks CS4 Beta
			[link] => http://www.pixel2life.com/tutorials/count/60914/introduction_to_tooltips_in_fireworks_cs4_beta/
			[guid] => http://www.pixel2life.com/tutorials/count/60914/introduction_to_tooltips_in_fireworks_cs4_beta/
			[comments] => http://www.pixel2life.com/comments/60914/introduction_to_tooltips_in_fireworks_cs4_beta/
			[description] =>  Introduction to Tooltips in Fireworks CS4 Beta
			[pubDate] => Sat, 14 Jun 2008 11:00 EDT
			[category] => Fireworks Tutorials
		)

	[3] => Array
		(
			[title] => Using the Measurements Auto Shape in Fireworks CS4 Beta
			[link] => http://www.pixel2life.com/tutorials/count/60913/using_the_measurements_auto_shape_in_fireworks_cs4_beta/
			[guid] => http://www.pixel2life.com/tutorials/count/60913/using_the_measurements_auto_shape_in_fireworks_cs4_beta/
			[comments] => http://www.pixel2life.com/comments/60913/using_the_measurements_auto_shape_in_fireworks_cs4_beta/
			[description] =>  Using the Measurements Auto Shape in Fireworks CS4 Beta
			[pubDate] => Sat, 14 Jun 2008 10:30 EDT
			[category] => Fireworks Tutorials
		)

	[4] => Array
		(
			[title] => 9-Slice Scaling Transform in Fireworks CS4 Beta
			[link] => http://www.pixel2life.com/tutorials/count/60912/9_slice_scaling_transform_in_fireworks_cs4_beta/
			[guid] => http://www.pixel2life.com/tutorials/count/60912/9_slice_scaling_transform_in_fireworks_cs4_beta/
			[comments] => http://www.pixel2life.com/comments/60912/9_slice_scaling_transform_in_fireworks_cs4_beta/
			[description] =>  9-Slice Scaling Transform in Fireworks CS4 Beta
			[pubDate] => Sat, 14 Jun 2008 10:00 EDT
			[category] => Fireworks Tutorials
		)

	[5] => Array
		(
			[title] => Creating the IE7 style logo effect in Fireworks
			[link] => http://www.pixel2life.com/tutorials/count/60219/creating_the_ie7_logo_effect_in_fireworks/
			[guid] => http://www.pixel2life.com/tutorials/count/60219/creating_the_ie7_logo_effect_in_fireworks/
			[comments] => http://www.pixel2life.com/comments/60219/creating_the_ie7_logo_effect_in_fireworks/
			[description] =>  Creating the IE7 style logo effect in Fireworks
			[pubDate] => Wed, 14 May 2008 11:30 EDT
			[category] => Fireworks Tutorials
		)

	[6] => Array
		(
			[title] => Creating the iPhone Interface on Fireworks
			[link] => http://www.pixel2life.com/tutorials/count/60218/creating_the_iphone_interface_on_fireworks/
			[guid] => http://www.pixel2life.com/tutorials/count/60218/creating_the_iphone_interface_on_fireworks/
			[comments] => http://www.pixel2life.com/comments/60218/creating_the_iphone_interface_on_fireworks/
			[description] =>  Creating the iPhone Interface on Fireworks
			[pubDate] => Wed, 14 May 2008 11:00 EDT
			[category] => Fireworks Tutorials
		)

	[7] => Array
		(
			[title] => Obama's Background Design in Fireworks - Part 1
			[link] => http://www.pixel2life.com/tutorials/count/60217/obama_s_background_on_fireworks_part_1/
			[guid] => http://www.pixel2life.com/tutorials/count/60217/obama_s_background_on_fireworks_part_1/
			[comments] => http://www.pixel2life.com/comments/60217/obama_s_background_on_fireworks_part_1/
			[description] =>  Obama's Background Design in Fireworks - Part 1
			[pubDate] => Wed, 14 May 2008 10:30 EDT
			[category] => Fireworks Tutorials
		)

	[8] => Array
		(
			[title] => Newspond menu designed in Fireworks - Part 1
			[link] => http://www.pixel2life.com/tutorials/count/60216/newspond_menu_on_fireworks_part_1/
			[guid] => http://www.pixel2life.com/tutorials/count/60216/newspond_menu_on_fireworks_part_1/
			[comments] => http://www.pixel2life.com/comments/60216/newspond_menu_on_fireworks_part_1/
			[description] =>  Newspond menu designed in Fireworks - Part 1
			[pubDate] => Wed, 14 May 2008 10:00 EDT
			[category] => Fireworks Tutorials
		)

	[9] => Array
		(
			[title] => Video Tutorial on Vector Cartoon Character Design
			[link] => http://www.pixel2life.com/tutorials/count/59245/video_tutorial_vector_cartoon_character_design/
			[guid] => http://www.pixel2life.com/tutorials/count/59245/video_tutorial_vector_cartoon_character_design/
			[comments] => http://www.pixel2life.com/comments/59245/video_tutorial_vector_cartoon_character_design/
			[description] =>  Video Tutorial on Vector Cartoon Character Design
			[pubDate] => Thu, 10 Apr 2008 19:30 EDT
			[category] => Fireworks Tutorials
		)

	[10] => Array
		(
			[title] => Video on Vector Drawing - Watch and learn to draw a brush out of vectors
			[link] => http://www.pixel2life.com/tutorials/count/58940/video_on_vector_drawing_watch_and_learn_to_draw_a_brush_out_of_vectors/
			[guid] => http://www.pixel2life.com/tutorials/count/58940/video_on_vector_drawing_watch_and_learn_to_draw_a_brush_out_of_vectors/
			[comments] => http://www.pixel2life.com/comments/58940/video_on_vector_drawing_watch_and_learn_to_draw_a_brush_out_of_vectors/
			[description] =>  Video on Vector Drawing - Watch and learn to draw a brush out of vectors
			[pubDate] => Mon, 31 Mar 2008 16:30 EDT
			[category] => Fireworks Tutorials
		)

	[11] => Array
		(
			[title] => Apple Something's in the Air Banner in Fireworks - Ad Creation
			[link] => http://www.pixel2life.com/tutorials/count/57367/apple_something_s_in_the_air_banner_in_fireworks/
			[guid] => http://www.pixel2life.com/tutorials/count/57367/apple_something_s_in_the_air_banner_in_fireworks/
			[comments] => http://www.pixel2life.com/comments/57367/apple_something_s_in_the_air_banner_in_fireworks/
			[description] =>  Apple Something's in the Air Banner in Fireworks - Ad Creation
			[pubDate] => Thu, 24 Jan 2008 12:00 EST
			[category] => Fireworks Tutorials
		)

	[12] => Array
		(
			[title] => Create Vista Style Wallpapers with Fireworks!
			[link] => http://www.pixel2life.com/tutorials/count/56853/create_vista_like_wallpapers_with_fireworks/
			[guid] => http://www.pixel2life.com/tutorials/count/56853/create_vista_like_wallpapers_with_fireworks/
			[comments] => http://www.pixel2life.com/comments/56853/create_vista_like_wallpapers_with_fireworks/
			[description] =>  Create Vista Style Wallpapers with Fireworks!
			[pubDate] => Sat, 05 Jan 2008 19:00 EST
			[category] => Fireworks Tutorials
		)

	[13] => Array
		(
			[title] => Create a Planet from Scratch with Fireworks
			[link] => http://www.pixel2life.com/tutorials/count/56378/create_a_planet_from_scratch_with_fireworks/
			[guid] => http://www.pixel2life.com/tutorials/count/56378/create_a_planet_from_scratch_with_fireworks/
			[comments] => http://www.pixel2life.com/comments/56378/create_a_planet_from_scratch_with_fireworks/
			[description] =>  Create a Planet from Scratch with Fireworks
			[pubDate] => Sun, 16 Dec 2007 02:00 EST
			[category] => Fireworks Tutorials
		)

	[14] => Array
		(
			[title] => How To Create A Thick Outline Around Text Video Tutorial
			[link] => http://www.pixel2life.com/tutorials/count/56258/how_to_create_a_thick_outline_around_text_video_tutorial/
			[guid] => http://www.pixel2life.com/tutorials/count/56258/how_to_create_a_thick_outline_around_text_video_tutorial/
			[comments] => http://www.pixel2life.com/comments/56258/how_to_create_a_thick_outline_around_text_video_tutorial/
			[description] =>  How To Create A Thick Outline Around Text Video Tutorial
			[pubDate] => Mon, 10 Dec 2007 09:30 EST
			[category] => Fireworks Tutorials
		)

	[15] => Array
		(
			[title] => How To Numerically Scale An Image In Fireworks Video Tutorial
			[link] => http://www.pixel2life.com/tutorials/count/56256/how_to_numerically_scale_an_image_in_fireworks_video_tutorial/
			[guid] => http://www.pixel2life.com/tutorials/count/56256/how_to_numerically_scale_an_image_in_fireworks_video_tutorial/
			[comments] => http://www.pixel2life.com/comments/56256/how_to_numerically_scale_an_image_in_fireworks_video_tutorial/
			[description] =>  How To Numerically Scale An Image In Fireworks Video Tutorial
			[pubDate] => Mon, 10 Dec 2007 09:00 EST
			[category] => Fireworks Tutorials
		)

	[16] => Array
		(
			[title] => How To Use The Rubber Stamp Tool Video Tutorial
			[link] => http://www.pixel2life.com/tutorials/count/56223/how_to_use_the_rubber_stamp_tool_video_tutorial/
			[guid] => http://www.pixel2life.com/tutorials/count/56223/how_to_use_the_rubber_stamp_tool_video_tutorial/
			[comments] => http://www.pixel2life.com/comments/56223/how_to_use_the_rubber_stamp_tool_video_tutorial/
			[description] =>  How To Use The Rubber Stamp Tool Video Tutorial
			[pubDate] => Sun, 09 Dec 2007 23:30 EST
			[category] => Fireworks Tutorials
		)

	[17] => Array
		(
			[title] => How To Onion Skin Frames In An Animation Video Tutorial
			[link] => http://www.pixel2life.com/tutorials/count/56221/how_to_onion_skin_frames_in_an_animation_video_tutorial/
			[guid] => http://www.pixel2life.com/tutorials/count/56221/how_to_onion_skin_frames_in_an_animation_video_tutorial/
			[comments] => http://www.pixel2life.com/comments/56221/how_to_onion_skin_frames_in_an_animation_video_tutorial/
			[description] =>  How To Onion Skin Frames In An Animation Video Tutorial
			[pubDate] => Sun, 09 Dec 2007 23:00 EST
			[category] => Fireworks Tutorials
		)

	[18] => Array
		(
			[title] => How To Use The Oval HotSpot Tool Video Tutorial
			[link] => http://www.pixel2life.com/tutorials/count/56219/how_to_use_the_oval_hotspot_tool_video_tutorial/
			[guid] => http://www.pixel2life.com/tutorials/count/56219/how_to_use_the_oval_hotspot_tool_video_tutorial/
			[comments] => http://www.pixel2life.com/comments/56219/how_to_use_the_oval_hotspot_tool_video_tutorial/
			[description] =>  How To Use The Oval HotSpot Tool Video Tutorial
			[pubDate] => Sun, 09 Dec 2007 22:30 EST
			[category] => Fireworks Tutorials
		)

	[19] => Array
		(
			[title] => How To Change The End Points Of A Line Path Video Tutorial
			[link] => http://www.pixel2life.com/tutorials/count/56217/how_to_change_the_end_points_of_a_line_path_video_tutorial/
			[guid] => http://www.pixel2life.com/tutorials/count/56217/how_to_change_the_end_points_of_a_line_path_video_tutorial/
			[comments] => http://www.pixel2life.com/comments/56217/how_to_change_the_end_points_of_a_line_path_video_tutorial/
			[description] =>  How To Change The End Points Of A Line Path Video Tutorial
			[pubDate] => Sun, 09 Dec 2007 22:00 EST
			[category] => Fireworks Tutorials
		)

)


#10 rc69

rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 17 September 2008 - 11:27 AM

Looks right to me. Don't know what you think is wrong, but maybe the following link will help clear things up for you:
print_r()

#11 UnrealMedia

UnrealMedia

    Young Padawan

  • Members
  • Pip
  • 72 posts

Posted 01 October 2008 - 10:49 AM

Hmm...I dont really see the advantages to this lol. There are better methods in my opinion.

#12 MoMisHere

MoMisHere

    Young Padawan

  • Members
  • Pip
  • 3 posts

Posted 04 April 2009 - 09:02 PM

Hmm...I dont really see the advantages to this lol. There are better methods in my opinion.


Care to point out some of these?

#13 Wybe

Wybe

    Jedi In Training

  • Members
  • PipPip
  • 401 posts
  • Gender:Male
  • Location:the Netherlands
  • Interests:I have no interests

Posted 16 April 2009 - 04:46 PM

agreed. what's easier than foreach(rss_to_array() as $whatever) { blablabla

#14 Dave Arison

Dave Arison

    Young Padawan

  • Members
  • Pip
  • 1 posts

Posted 25 April 2009 - 09:02 AM

How do you solve the cache problem ?

If you run this script with different of rss feeds one after another that you will always get the same feed.

I used it at a part of my script and don't know how to solve it ?

Thanks

#15 rc69

rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 25 April 2009 - 05:18 PM

The subject of how to implement a cache is probably beyond the scope of this tutorial. However, in an attempt to have an efficient RSS feed reader, i just used the same thing that Wordpress uses: Magpie RSS. It has built in caching.

#16 Hayden

Hayden

    P2L Jedi

  • Members
  • PipPipPip
  • 717 posts
  • Gender:Male
  • Location:Texas

Posted 05 May 2009 - 02:25 PM

$rss_tags = array(
		'title',
		'link',
		'guid',
		'comments',
		'description',
		'pubDate',
		'category',
	);

Did anybody else notice a problem here? Or was this so obvious that everyone just fixed it and moved on?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users