Php: Rss/xml Парсер - Может кому надо

Discussion in 'PHP' started by Utochka, 20 Apr 2006.

  1. Utochka

    Utochka Elder - Старейшина

    Joined:
    21 Dec 2005
    Messages:
    495
    Likes Received:
    106
    Reputations:
    54
    PHP:
    <?php
    $lenta 
    = $_GET['lenta'];
    $xmlfile = $_GET['xmlfile'];

    set_time_limit(0);

    $file = "http://$lenta/$xmlfile";
    $rss_channel = array();
    $currently_writing = "";
    $main = "";
    $item_counter = 0;
    function 
    startElement($parser, $name, $attrs) {
           global 
    $rss_channel, $currently_writing, $main;
           switch(
    $name) {
               case 
    "RSS":
               case 
    "RDF:RDF":
               case 
    "ITEMS":
                   
    $currently_writing = "";
                   break;
               case 
    "CHANNEL":
                   
    $main = "CHANNEL";
                   break;
               case 
    "IMAGE":
                   
    $main = "IMAGE";
                   
    $rss_channel["IMAGE"] = array();
                   break;
               case 
    "ITEM":
                   
    $main = "ITEMS";
                   break;
               default:
                   
    $currently_writing = $name;
                   break;
           }
    }
    function 
    endElement($parser, $name) {
           global 
    $rss_channel, $currently_writing, $item_counter;
           
    $currently_writing = "";
           if (
    $name == "ITEM") {
               
    $item_counter++;
           }
    }
    function 
    characterData($parser, $data) {
        global 
    $rss_channel, $currently_writing, $main, $item_counter;
        if (
    $currently_writing != "") {
            switch(
    $main) {
                case 
    "CHANNEL":
                    if (isset(
    $rss_channel[$currently_writing])) {
                        
    $rss_channel[$currently_writing] .= $data;
                    } else {
                        
    $rss_channel[$currently_writing] = $data;
                    }
                    break;
                case 
    "IMAGE":
                    if (isset(
    $rss_channel[$main][$currently_writing])) {
                        
    $rss_channel[$main][$currently_writing] .= $data;
                    } else {
                        
    $rss_channel[$main][$currently_writing] = $data;
                    }
                    break;
                case 
    "ITEMS":
                    if (isset(
    $rss_channel[$main][$item_counter][$currently_writing])) {
                        
    $rss_channel[$main][$item_counter][$currently_writing] .= $data;
                    } else {
                        
    //print ("rss_channel[$main][$item_counter][$currently_writing] = $data");
                        
    $rss_channel[$main][$item_counter][$currently_writing] = $data;
                    }
                    break;
            }
        }
    }
    $xml_parser = xml_parser_create();
    xml_set_element_handler($xml_parser, "startElement", "endElement");
    xml_set_character_data_handler($xml_parser, "characterData");
    if (!(
    $fp = fopen($file, "r"))) {
        die(
    "could not open XML input");
    }
    while (
    $data = fread($fp, 4096)) {
        if (!
    xml_parse($xml_parser, $data, feof($fp))) {
            die(
    sprintf("XML error: %s at line %d",
                        
    xml_error_string(xml_get_error_code($xml_parser)),
                        
    xml_get_current_line_number($xml_parser)));
        }
    }
    xml_parser_free($xml_parser);
    if (isset(
    $rss_channel["ITEMS"])) {
        if (
    count($rss_channel["ITEMS"]) > 0) {
            for(
    $i = 0;$i < count($rss_channel["ITEMS"]);$i++) {
                
    $title = $rss_channel["ITEMS"][$i]["TITLE"];
                
    $dest = $rss_channel["ITEMS"][$i]["DESCRIPTION"];
    //  Тут можно сделать вывод на страницу, или ввод в БД
    // $rss_channel["ITEMS"][$i]["элемент"];
            
    }
        } else {
            print (
    "<b>Нет ничего.</b>");
        }
    }
    exit;
    ?> 
     
  2. Talisman

    Talisman Elder - Старейшина

    Joined:
    22 Apr 2006
    Messages:
    400
    Likes Received:
    151
    Reputations:
    80
    Вери биг сенкс! (уже сам половину написал, и тут твою нашел :) )
     
  3. nerezus

    nerezus Banned

    Joined:
    12 Aug 2004
    Messages:
    3,191
    Likes Received:
    727
    Reputations:
    266
    ой, а не легче ли просто сделать так:

    PHP:
    <?php 
    /* 
    ====================================================================== 
    lastRSS usage DEMO 3 - Simple RSS agregator 
    ---------------------------------------------------------------------- 
    This example shows, how to create simple RSS agregator 
        - create lastRSS object 
        - set transparent cache 
        - show a few RSS files at once 
    ====================================================================== 
    */ 

    function ShowOneRSS($url) { 
        global 
    $rss; 
        if (
    $rs = $rss->get($url)) { 
            echo 
    "<big><b><a href=\"$rs[link]\">$rs[title]</a></b></big><br />\n"; 
            echo 
    "$rs[description]<br />\n"; 

                echo 
    "<ul>\n"; 
                foreach (
    $rs['items'] as $item) { 
                    echo 
    "\t<li><a href=\"$item[link]\" title=\"$item[description]\">$item[title]</a></li>\n"; 
                } 
                if (
    $rs['items_count'] <= 0) { echo "<li>Sorry, no items found in the RSS file :-(</li>"; } 
                echo 
    "</ul>\n"; 
        } 
        else { 
            echo 
    "Sorry: It's not possible to reach RSS file $url\n<br />"; 
            
    // you will probably hide this message in a live version 
        
    } 
    } 

    // =============================================================================== 

    // include lastRSS 
    include "./lastRSS.php"; 

    // List of RSS URLs 
    $rss_left = array( 
        
    'http://freshmeat.net/backend/fm.rdf', 
        
    'http://slashdot.org/slashdot.rdf' 
    ); 
    $rss_right = array( 
        
    'http://www.freshfolder.com/rss.php', 
        
    'http://phpbuilder.com/rss_feed.php' 
    ); 

    // Create lastRSS object 
    $rss = new lastRSS; 

    // Set cache dir and cache time limit (5 seconds) 
    // (don't forget to chmod cahce dir to 777 to allow writing) 
    $rss->cache_dir = './temp'; 
    $rss->cache_time = 1200; 


    // Show all rss files 
    echo "<table cellpadding=\"10\" border=\"0\"><tr><td width=\"50%\" valign=\"top\">"; 
    foreach (
    $rss_left as $url) { 
        
    ShowOneRSS($url); 
    } 
    echo 
    "</td><td width=\"50%\" valign=\"top\">"; 
    foreach (
    $rss_right as $url) { 
        
    ShowOneRSS($url); 
    } 
    echo 
    "</td></tr></table>"; 
    ?>

     
    1 person likes this.
  4. nerezus

    nerezus Banned

    Joined:
    12 Aug 2004
    Messages:
    3,191
    Likes Received:
    727
    Reputations:
    266
    lastRSS - зачотный модуль )
     
  5. Trinux

    Trinux Members of Antichat

    Joined:
    26 Nov 2004
    Messages:
    1,403
    Likes Received:
    296
    Reputations:
    364
    _________________________
  6. Talisman

    Talisman Elder - Старейшина

    Joined:
    22 Apr 2006
    Messages:
    400
    Likes Received:
    151
    Reputations:
    80
  7. nerezus

    nerezus Banned

    Joined:
    12 Aug 2004
    Messages:
    3,191
    Likes Received:
    727
    Reputations:
    266
    Talisman, докатились. Вбей в гугл это слово и нажми на первую ссылку.
     
  8. Trinux

    Trinux Members of Antichat

    Joined:
    26 Nov 2004
    Messages:
    1,403
    Likes Received:
    296
    Reputations:
    364
    2-мя постами выше =)))))))))))))))))))))))))))))))
     
    _________________________
  9. Talisman

    Talisman Elder - Старейшина

    Joined:
    22 Apr 2006
    Messages:
    400
    Likes Received:
    151
    Reputations:
    80
    Спасибо :) а я не в первую тыкал... т.е. не сразу нашел http://lastrss.webdot.cz/ :)