Help - Search - Members - Calendar
Full Version: php rss to array function
Pixel2Life Forum > Member Tutorials and Requests > Forum Tutorial Archives > PHP Tutorials
curthard89
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

CODE
<?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
Corey
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.
curthard89
i copied exactly what i put and just tried it...works fine...lol, ive attached an image....and wala...
Corey
QUOTE(curthard89 @ Jul 29 2007, 05:11 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.
curthard89
running 5, tried at work on our server which runs 4 and works fine aswell hmmm
Corey
That is really weird then. hmm
101key
nice
rc69
The DOMdocument class is a PHP5 class that is not available in PHP4 (ref: http://php.net/manual/en/domdocument.construct.php).

Object iteration is also only available in PHP5 (ref: http://php.net/manual/en/language.oop5.iterations.php).

So technically speaking, the error Corey got should have been at the following line:
CODE
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 bigwink.gif
UnrealMedia
Its not parsing...unless this is how its suposed to be...

CODE
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
        )

)
rc69
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()
UnrealMedia
Hmm...I dont really see the advantages to this lol. There are better methods in my opinion.
MoMisHere
QUOTE (UnrealMedia @ Oct 1 2008, 10:49 AM) *
Hmm...I dont really see the advantages to this lol. There are better methods in my opinion.


Care to point out some of these?
Wybe
agreed. what's easier than foreach(rss_to_array() as $whatever) { blablabla
Dave Arison
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
rc69
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.
Hayden
CODE
$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?
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.