ugrás a tartalomhoz

RSS hírek naplózása

lotanujo · 2011. Jan. 26. (Sze), 20.50
Sziasztok!

Már van egy jól működő RSS hírolvasó a honlapomon. Ezt szeretném úgy átalakítani, hogy a friss híreket mindig naplózza el MySQL adatbázisba.
Hogyan lehetne ezt megoldani az alábbi kód átalakításával?

Ez jeleníti meg a hírt a honlapon:

<?php
require_once ('rss/rss_fetch.inc');
$url = 'http://www.dunatv.hu/rss/rsschannel?channelid=1082';
$rss = @fetch_rss($url);
//forrás,cím 0-1,tartalom 0-1,tartalom char 0-~,hírek száma 0-x, style css-s idje, javascript 0-1, link tájolása 0-1, sortörés, hírek közötti törés
@fetchrss($rss,1,0,0,10,"hir1",0,1,"","<hr color=\"#9A9A9A\" noShade size=\"1\" width=\"100%\">");
?>



rss_fetch.inc fájl

<?php
include("dbconn.php");


/*
 * Project:     MagpieRSS: a simple RSS integration tool
 * File:        rss_fetch.inc, a simple functional interface
                to fetching and parsing RSS files, via the
                function fetch_rss()
 * Author:      Kellan Elliott-McCrea <kellan##kukac##protest.net>
 * License:     GPL
 *
 * The lastest version of MagpieRSS can be obtained from:
 * http://magpierss.sourceforge.net
 *
 * For questions, help, comments, discussion, etc., please join the
 * Magpie mailing list:
 * magpierss-general##kukac##lists.sourceforge.net
 *
 */
 
// Setup MAGPIE_DIR for use on hosts that don't include
// the current path in include_path.
// with thanks to rajiv and smarty
if (!defined('DIR_SEP')) {
    define('DIR_SEP', DIRECTORY_SEPARATOR);
}

if (!defined('MAGPIE_DIR')) {
    define('MAGPIE_DIR', dirname(__FILE__) . DIR_SEP);
}

require_once( MAGPIE_DIR . 'rss_parse.inc' );
require_once( MAGPIE_DIR . 'rss_cache.inc' );

// for including 3rd party libraries
define('MAGPIE_EXTLIB', MAGPIE_DIR . 'extlib' . DIR_SEP);
require_once( MAGPIE_EXTLIB . 'Snoopy.class.inc');


/*
 * CONSTANTS - redefine these in your script to change the
 * behaviour of fetch_rss() currently, most options effect the cache
 *
 * MAGPIE_CACHE_ON - Should Magpie cache parsed RSS objects?
 * For me a built in cache was essential to creating a "PHP-like"
 * feel to Magpie, see rss_cache.inc for rationale
 *
 *
 * MAGPIE_CACHE_DIR - Where should Magpie cache parsed RSS objects?
 * This should be a location that the webserver can write to.   If this
 * directory does not already exist Mapie will try to be smart and create
 * it.  This will often fail for permissions reasons.
 *
 *
 * MAGPIE_CACHE_AGE - How long to store cached RSS objects? In seconds.
 *
 *
 * MAGPIE_CACHE_FRESH_ONLY - If remote fetch fails, throw error
 * instead of returning stale object?
 *
 * MAGPIE_DEBUG - Display debugging notices?
 *
*/


/*=======================================================================*\
    Function: fetch_rss:
    Purpose:  return RSS object for the give url
              maintain the cache
    Input:    url of RSS file
    Output:   parsed RSS object (see rss_parse.inc)

    NOTES ON CACHEING:  
    If caching is on (MAGPIE_CACHE_ON) fetch_rss will first check the cache.
   
    NOTES ON RETRIEVING REMOTE FILES:
    If conditional gets are on (MAGPIE_CONDITIONAL_GET_ON) fetch_rss will
    return a cached object, and touch the cache object upon recieving a
    304.
   
    NOTES ON FAILED REQUESTS:
    If there is an HTTP error while fetching an RSS object, the cached
    version will be return, if it exists (and if MAGPIE_CACHE_FRESH_ONLY is off)
\*=======================================================================*/

define('MAGPIE_VERSION', '0.72');
define('MAGPIE_CACHE_DIR','/home/web/domains/mappa/cache');

$MAGPIE_ERROR = "";

function fetch_rss ($url) {
    // initialize constants
    init();
   
    if ( !isset($url) ) {
        error("fetch_rss called without a url");
        return false;
    }
   
    // if cache is disabled
    if ( !MAGPIE_CACHE_ON ) {
        // fetch file, and parse it
        $resp = _fetch_remote_file( $url );
        if ( is_success( $resp->status ) ) {
            return _response_to_rss( $resp );
        }
        else {
            error("Failed to fetch $url and cache is off");
            return false;
        }
    }
    // else cache is ON
    else {
        // Flow
        // 1. check cache
        // 2. if there is a hit, make sure its fresh
        // 3. if cached obj fails freshness check, fetch remote
        // 4. if remote fails, return stale object, or error
       
        $cache = new RSSCache( MAGPIE_CACHE_DIR, MAGPIE_CACHE_AGE );
       
        if (MAGPIE_DEBUG and $cache->ERROR) {
            debug($cache->ERROR, E_USER_WARNING);
        }
       
       
        $cache_status    = 0;       // response of check_cache
        $request_headers = array(); // HTTP headers to send with fetch
        $rss             = 0;       // parsed RSS object
        $errormsg        = 0;       // errors, if any
       
        // store parsed XML by desired output encoding
        // as character munging happens at parse time
        $cache_key       = $url . MAGPIE_OUTPUT_ENCODING;
       
        if (!$cache->ERROR) {
            // return cache HIT, MISS, or STALE
            $cache_status = $cache->check_cache( $cache_key);
        }
               
        // if object cached, and cache is fresh, return cached obj
        if ( $cache_status == 'HIT' ) {
            $rss = $cache->get( $cache_key );
            if ( isset($rss) and $rss ) {
                // should be cache age
                $rss->from_cache = 1;
                if ( MAGPIE_DEBUG > 1) {
                    debug("MagpieRSS: Cache HIT", E_USER_NOTICE);
                }
                return $rss;
            }
        }
       
        // else attempt a conditional get
       
        // setup headers
        if ( $cache_status == 'STALE' ) {
            $rss = $cache->get( $cache_key );
            if ( $rss and $rss->etag and $rss->last_modified ) {
                $request_headers['If-None-Match'] = $rss->etag;
                $request_headers['If-Last-Modified'] = $rss->last_modified;
            }
        }
       
        $resp = _fetch_remote_file( $url, $request_headers );
       
        if (isset($resp) and $resp) {
          if ($resp->status == '304' ) {
                // we have the most current copy
                if ( MAGPIE_DEBUG > 1) {
                    debug("Got 304 for $url");
                }
                // reset cache on 304 (at minutillo insistent prodding)
                $cache->set($cache_key, $rss);
                return $rss;
            }
            elseif ( is_success( $resp->status ) ) {
                $rss = _response_to_rss( $resp );
                if ( $rss ) {
                    if (MAGPIE_DEBUG > 1) {
                        debug("Fetch successful");
                    }
                    // add object to cache
                    $cache->set( $cache_key, $rss );
                    return $rss;
                }
            }
            else {
                $errormsg = "Failed to fetch $url ";
                if ( $resp->status == '-100' ) {
                    $errormsg .= "(Request timed out after " . MAGPIE_FETCH_TIME_OUT . " seconds)";
                }
                elseif ( $resp->error ) {
                    # compensate for Snoopy's annoying habbit to tacking
                    # on '\n'
                    $http_error = substr($resp->error, 0, -2);
                    $errormsg .= "(HTTP Error: $http_error)";
                }
                else {
                    $errormsg .=  "(HTTP Response: " . $resp->response_code .')';
                }
            }
        }
        else {
            $errormsg = "Unable to retrieve RSS file for unknown reasons.";
        }
       
        // else fetch failed
       
        // attempt to return cached object
        if ($rss) {
            if ( MAGPIE_DEBUG ) {
                debug("Returning STALE object for $url");
            }
            return $rss;
        }
       
        // else we totally failed
        error( $errormsg );
       
        return false;
       
    } // end if ( !MAGPIE_CACHE_ON ) {
} // end fetch_rss()

/*=======================================================================*\
    Function:   error
    Purpose:    set MAGPIE_ERROR, and trigger error
\*=======================================================================*/

function error ($errormsg, $lvl=E_USER_WARNING) {
        global $MAGPIE_ERROR;
       
        // append PHP's error message if track_errors enabled
        if ( isset($php_errormsg) ) {
            $errormsg .= " ($php_errormsg)";
        }
        if ( $errormsg ) {
            $errormsg = "MagpieRSS: $errormsg";
            $MAGPIE_ERROR = $errormsg;
            trigger_error( $errormsg, $lvl);                
        }
}

function debug ($debugmsg, $lvl=E_USER_NOTICE) {
    trigger_error("MagpieRSS [debug] $debugmsg", $lvl);
}
           
/*=======================================================================*\
    Function:   magpie_error
    Purpose:    accessor for the magpie error variable
\*=======================================================================*/
function magpie_error ($errormsg="") {
    global $MAGPIE_ERROR;
   
    if ( isset($errormsg) and $errormsg ) {
        $MAGPIE_ERROR = $errormsg;
    }
   
    return $MAGPIE_ERROR;  
}

/*=======================================================================*\
    Function:   _fetch_remote_file
    Purpose:    retrieve an arbitrary remote file
    Input:      url of the remote file
                headers to send along with the request (optional)
    Output:     an HTTP response object (see Snoopy.class.inc)  
\*=======================================================================*/
function _fetch_remote_file ($url, $headers = "" ) {
    // Snoopy is an HTTP client in PHP
    $client = new Snoopy();
    $client->agent = MAGPIE_USER_AGENT;
    $client->read_timeout = MAGPIE_FETCH_TIME_OUT;
    $client->use_gzip = MAGPIE_USE_GZIP;
    if (is_array($headers) ) {
        $client->rawheaders = $headers;
    }
   
    @$client->fetch($url);
    return $client;

}

/*=======================================================================*\
    Function:   _response_to_rss
    Purpose:    parse an HTTP response object into an RSS object
    Input:      an HTTP response object (see Snoopy)
    Output:     parsed RSS object (see rss_parse)
\*=======================================================================*/
function _response_to_rss ($resp) {
    $rss = new MagpieRSS( $resp->results, MAGPIE_OUTPUT_ENCODING, MAGPIE_INPUT_ENCODING, MAGPIE_DETECT_ENCODING );
   
    // if RSS parsed successfully      
    if ( $rss and !$rss->ERROR) {
       
        // find Etag, and Last-Modified
        foreach($resp->headers as $h) {
            // 2003-03-02 - Nicola Asuni (www.tecnick.com) - fixed bug "Undefined offset: 1"
            if (strpos($h, ": ")) {
                list($field, $val) = explode(": ", $h, 2);
            }
            else {
                $field = $h;
                $val = "";
            }
           
            if ( $field == 'ETag' ) {
                $rss->etag = $val;
            }
           
            if ( $field == 'Last-Modified' ) {
                $rss->last_modified = $val;
            }
        }
       
        return $rss;    
    } // else construct error message
    else {
        $errormsg = "Failed to parse RSS file.";
       
        if ($rss) {
            $errormsg .= " (" . $rss->ERROR . ")";
        }
        error($errormsg);
       
        return false;
    } // end if ($rss and !$rss->error)
}

/*=======================================================================*\
    Function:   init
    Purpose:    setup constants with default values
                check for user overrides
\*=======================================================================*/
function init () {
    if ( defined('MAGPIE_INITALIZED') ) {
        return;
    }
    else {
        define('MAGPIE_INITALIZED', true);
    }
   
    if ( !defined('MAGPIE_CACHE_ON') ) {
        define('MAGPIE_CACHE_ON', true);
    }

    if ( !defined('MAGPIE_CACHE_DIR') ) {
        define('MAGPIE_CACHE_DIR', './cache');
    }

    if ( !defined('MAGPIE_CACHE_AGE') ) {
        define('MAGPIE_CACHE_AGE', 60*60); // one hour
    }

    if ( !defined('MAGPIE_CACHE_FRESH_ONLY') ) {
        define('MAGPIE_CACHE_FRESH_ONLY', false);
    }

    if ( !defined('MAGPIE_OUTPUT_ENCODING') ) {
        define('MAGPIE_OUTPUT_ENCODING', 'ISO-8859-1');
    }
   
    if ( !defined('MAGPIE_INPUT_ENCODING') ) {
        define('MAGPIE_INPUT_ENCODING', null);
    }
   
    if ( !defined('MAGPIE_DETECT_ENCODING') ) {
        define('MAGPIE_DETECT_ENCODING', true);
    }
   
    if ( !defined('MAGPIE_DEBUG') ) {
        define('MAGPIE_DEBUG', 0);
    }
   
    if ( !defined('MAGPIE_USER_AGENT') ) {
        $ua = 'MagpieRSS/'. MAGPIE_VERSION . ' (+http://magpierss.sf.net';
       
        if ( MAGPIE_CACHE_ON ) {
            $ua = $ua . ')';
        }
        else {
            $ua = $ua . '; No cache)';
        }
       
        define('MAGPIE_USER_AGENT', $ua);
    }
   
    if ( !defined('MAGPIE_FETCH_TIME_OUT') ) {
        define('MAGPIE_FETCH_TIME_OUT', 5); // 5 second timeout
    }
   
    // use gzip encoding to fetch rss files if supported?
    if ( !defined('MAGPIE_USE_GZIP') ) {
        define('MAGPIE_USE_GZIP', true);    
    }
}
function code2code_($data)
{
$kulcs[0]='/-1-/';
$kulcs[1]='/-2-/';
$kulcs[2]='/-3-/';
$kulcs[3]='/-4-/';

$kulcs_[0]='&#337;';
$kulcs_[1]='&#369;';
$kulcs_[2]='&#336;';
$kulcs_[3]='&#368;';
   

    return mb_convert_encoding($data,"ISO-8859-2","auto");
}
function fetchrss($rss,$hir_cim=1,$hir_body=1,$hir_body_char=100,$rss_count=0,$style_id="",$js=0,$target=0,$sortores="<br>",$hirtores="<hr/>")
    {
echo "<div id=\"$style_id\">";
//echo $rss->results;
$i=0;
foreach($rss->items as $item)
   {
$item["description"]=code2code_($item["description"]);
$item["summary"]=code2code_($item["summary"]);
$item["title"]=code2code_($item["title"]);



   if($item["title"]=="" || $item["description"]=="" || $item["link"]=="" || $item["summary"]=="")
   {
   //ha hibás a rekord, skippelünk
   continue;
   }
   else
   {
   //minden ok, lehet formázni
   
if($hir_cim==1)
{

   if($target==1)
{$igaz="target=\"_BLANK\"";}
else if($target==1)
{$igaz="";}
echo "<a href=\"".$item["link"]."\" ".$igaz.">".$item["title"]." -</a>".$sortores;

}


if($hir_body==1)
{
   if($hir_body_char==0)
{
echo $item["summary"]."".$sortores;
}
else
{
if(strlen($item["summary"])>$hir_body_char)
   {
   $buffer="";
   //szükséges a kicsinyítés
for($j=0;$j<$hir_body_char;$j++)
   {
   $buffer.=$item["summary"][$j];
   }
   $buffer.="...";
echo $buffer.$sortores;
   }
   else
   {
   //itt nem
   echo $item["summary"].$sortores;
   }
   


}
}
   
$published = parse_w3cdtf($item['dc']['date']);
//echo date("h:i:s", $published);
   //print_r($item);
   }
   //megallasi feltetel
   $i++;
   if($i>=$rss_count && $rss_count!=0)
{
break;
}
else
{
print($hirtores);
}
   }
echo "</div>";


    }



// NOTE: the following code should really be in Snoopy, or at least
// somewhere other then rss_fetch!

/*=======================================================================*\
    HTTP STATUS CODE PREDICATES
    These functions attempt to classify an HTTP status code
    based on RFC 2616 and RFC 2518.
   
    All of them take an HTTP status code as input, and return true or false

    All this code is adapted from LWP's HTTP::Status.
\*=======================================================================*/


/*=======================================================================*\
    Function:   is_info
    Purpose:    return true if Informational status code
\*=======================================================================*/
function is_info ($sc) {
    return $sc >= 100 && $sc < 200;
}

/*=======================================================================*\
    Function:   is_success
    Purpose:    return true if Successful status code
\*=======================================================================*/
function is_success ($sc) {
    return $sc >= 200 && $sc < 300;
}

/*=======================================================================*\
    Function:   is_redirect
    Purpose:    return true if Redirection status code
\*=======================================================================*/
function is_redirect ($sc) {
    return $sc >= 300 && $sc < 400;
}

/*=======================================================================*\
    Function:   is_error
    Purpose:    return true if Error status code
\*=======================================================================*/
function is_error ($sc) {
    return $sc >= 400 && $sc < 600;
}

/*=======================================================================*\
    Function:   is_client_error
    Purpose:    return true if Error status code, and its a client error
\*=======================================================================*/
function is_client_error ($sc) {
    return $sc >= 400 && $sc < 500;
}

/*=======================================================================*\
    Function:   is_client_error
    Purpose:    return true if Error status code, and its a server error
\*=======================================================================*/
function is_server_error ($sc) {
    return $sc >= 500 && $sc < 600;
}

mysql_query("INSERT INTO rsshir SET hirurl='".$item["link"]."',hircim='".$item["title"]."',hirszoveg='".$item["summary"]."',datum='".$published."'") or die(mysql_error());


mysql_close($kapcsolat);
?>


Itt a kód végén az INSERT INTO csak üres sorokat vesz fel, ha a kód belsejébe próbáltam el elhelyezni, akkor pedig a weboldalon sem jelentek meg a hírek.
 
1

Használj színezőt

Poetro · 2011. Jan. 26. (Sze), 21.06
Mivel nem használtál színezőt, így csak azt tudom írni, hogy a
  1. $cache->set( $cache_key$rss );  
sor előtt vagy után írd ki a $rss elemeit az adatbázisba.
2

Nem nyert

lotanujo · 2011. Jan. 26. (Sze), 21.22
Ha elő tettem be, akkor még az oldal is hibás lett, utána elhelyezve egy üres sort vett csak fel az adatbázisba.
  1. <?php  
  2. include("dbconn.php");  
  3.   
  4.   
  5. /* 
  6.  * Project:     MagpieRSS: a simple RSS integration tool 
  7.  * File:        rss_fetch.inc, a simple functional interface 
  8.                 to fetching and parsing RSS files, via the 
  9.                 function fetch_rss() 
  10.  * Author:      Kellan Elliott-McCrea <kellan##kukac##protest.net> 
  11.  * License:     GPL 
  12.  * 
  13.  * The lastest version of MagpieRSS can be obtained from: 
  14.  * http://magpierss.sourceforge.net 
  15.  * 
  16.  * For questions, help, comments, discussion, etc., please join the 
  17.  * Magpie mailing list: 
  18.  * magpierss-general##kukac##lists.sourceforge.net 
  19.  * 
  20.  */  
  21.    
  22. // Setup MAGPIE_DIR for use on hosts that don't include  
  23. // the current path in include_path.  
  24. // with thanks to rajiv and smarty  
  25. if (!defined('DIR_SEP')) {  
  26.     define('DIR_SEP', DIRECTORY_SEPARATOR);  
  27. }  
  28.   
  29. if (!defined('MAGPIE_DIR')) {  
  30.     define('MAGPIE_DIR', dirname(__FILE__) . DIR_SEP);  
  31. }  
  32.   
  33. require_once( MAGPIE_DIR . 'rss_parse.inc' );  
  34. require_once( MAGPIE_DIR . 'rss_cache.inc' );  
  35.   
  36. // for including 3rd party libraries  
  37. define('MAGPIE_EXTLIB', MAGPIE_DIR . 'extlib' . DIR_SEP);  
  38. require_once( MAGPIE_EXTLIB . 'Snoopy.class.inc');  
  39.   
  40.   
  41. /*  
  42.  * CONSTANTS - redefine these in your script to change the 
  43.  * behaviour of fetch_rss() currently, most options effect the cache 
  44.  * 
  45.  * MAGPIE_CACHE_ON - Should Magpie cache parsed RSS objects?  
  46.  * For me a built in cache was essential to creating a "PHP-like"  
  47.  * feel to Magpie, see rss_cache.inc for rationale 
  48.  * 
  49.  * 
  50.  * MAGPIE_CACHE_DIR - Where should Magpie cache parsed RSS objects? 
  51.  * This should be a location that the webserver can write to.   If this  
  52.  * directory does not already exist Mapie will try to be smart and create  
  53.  * it.  This will often fail for permissions reasons. 
  54.  * 
  55.  * 
  56.  * MAGPIE_CACHE_AGE - How long to store cached RSS objects? In seconds. 
  57.  * 
  58.  * 
  59.  * MAGPIE_CACHE_FRESH_ONLY - If remote fetch fails, throw error 
  60.  * instead of returning stale object? 
  61.  * 
  62.  * MAGPIE_DEBUG - Display debugging notices? 
  63.  * 
  64. */  
  65.   
  66.   
  67. /*=======================================================================*\ 
  68.     Function: fetch_rss:  
  69.     Purpose:  return RSS object for the give url 
  70.               maintain the cache 
  71.     Input:    url of RSS file 
  72.     Output:   parsed RSS object (see rss_parse.inc) 
  73.  
  74.     NOTES ON CACHEING:   
  75.     If caching is on (MAGPIE_CACHE_ON) fetch_rss will first check the cache. 
  76.      
  77.     NOTES ON RETRIEVING REMOTE FILES: 
  78.     If conditional gets are on (MAGPIE_CONDITIONAL_GET_ON) fetch_rss will 
  79.     return a cached object, and touch the cache object upon recieving a 
  80.     304. 
  81.      
  82.     NOTES ON FAILED REQUESTS: 
  83.     If there is an HTTP error while fetching an RSS object, the cached 
  84.     version will be return, if it exists (and if MAGPIE_CACHE_FRESH_ONLY is off) 
  85. \*=======================================================================*/  
  86.   
  87. define('MAGPIE_VERSION''0.72');  
  88. define('MAGPIE_CACHE_DIR','/home/web/domains/mappa/cache');  
  89.   
  90. $MAGPIE_ERROR = "";  
  91.   
  92. function fetch_rss ($url) {  
  93.     // initialize constants  
  94.     init();  
  95.       
  96.     if ( !isset($url) ) {  
  97.         error("fetch_rss called without a url");  
  98.         return false;  
  99.     }  
  100.       
  101.     // if cache is disabled  
  102.     if ( !MAGPIE_CACHE_ON ) {  
  103.         // fetch file, and parse it  
  104.         $resp = _fetch_remote_file( $url );  
  105.         if ( is_success( $resp->status ) ) {  
  106.             return _response_to_rss( $resp );  
  107.         }  
  108.         else {  
  109.             error("Failed to fetch $url and cache is off");  
  110.             return false;  
  111.         }  
  112.     }   
  113.     // else cache is ON  
  114.     else {  
  115.         // Flow  
  116.         // 1. check cache  
  117.         // 2. if there is a hit, make sure its fresh  
  118.         // 3. if cached obj fails freshness check, fetch remote  
  119.         // 4. if remote fails, return stale object, or error  
  120.           
  121.         $cache = new RSSCache( MAGPIE_CACHE_DIR, MAGPIE_CACHE_AGE );  
  122.           
  123.         if (MAGPIE_DEBUG and $cache->ERROR) {  
  124.             debug($cache->ERROR, E_USER_WARNING);  
  125.         }  
  126.           
  127.           
  128.         $cache_status    = 0;       // response of check_cache  
  129.         $request_headers = array(); // HTTP headers to send with fetch  
  130.         $rss             = 0;       // parsed RSS object  
  131.         $errormsg        = 0;       // errors, if any  
  132.           
  133.         // store parsed XML by desired output encoding  
  134.         // as character munging happens at parse time  
  135.         $cache_key       = $url . MAGPIE_OUTPUT_ENCODING;  
  136.           
  137.         if (!$cache->ERROR) {  
  138.             // return cache HIT, MISS, or STALE  
  139.             $cache_status = $cache->check_cache( $cache_key);  
  140.         }  
  141.                   
  142.         // if object cached, and cache is fresh, return cached obj  
  143.         if ( $cache_status == 'HIT' ) {  
  144.             $rss = $cache->get( $cache_key );  
  145.             if ( isset($rssand $rss ) {  
  146.                 // should be cache age  
  147.                 $rss->from_cache = 1;  
  148.                 if ( MAGPIE_DEBUG > 1) {  
  149.                     debug("MagpieRSS: Cache HIT", E_USER_NOTICE);  
  150.                 }  
  151.                 return $rss;  
  152.             }  
  153.         }  
  154.           
  155.         // else attempt a conditional get  
  156.           
  157.         // setup headers  
  158.         if ( $cache_status == 'STALE' ) {  
  159.             $rss = $cache->get( $cache_key );  
  160.             if ( $rss and $rss->etag and $rss->last_modified ) {  
  161.                 $request_headers['If-None-Match'] = $rss->etag;  
  162.                 $request_headers['If-Last-Modified'] = $rss->last_modified;  
  163.             }  
  164.         }  
  165.           
  166.         $resp = _fetch_remote_file( $url$request_headers );  
  167.           
  168.         if (isset($respand $resp) {  
  169.           if ($resp->status == '304' ) {  
  170.                 // we have the most current copy  
  171.                 if ( MAGPIE_DEBUG > 1) {  
  172.                     debug("Got 304 for $url");  
  173.                 }  
  174.                 // reset cache on 304 (at minutillo insistent prodding)  
  175.                 $cache->set($cache_key$rss);  
  176.                 return $rss;  
  177.             }  
  178.               
  179.             elseif ( is_success( $resp->status ) ) {  
  180.                 $rss = _response_to_rss( $resp );  
  181.                 if ( $rss ) {  
  182.                     if (MAGPIE_DEBUG > 1) {  
  183.                         debug("Fetch successful");  
  184.                     }  
  185.                     // add object to cache  
  186.                     $cache->set( $cache_key$rss );  
  187.                     return $rss;  
  188.                 }  
  189.             }  
  190.             else {  
  191.                 $errormsg = "Failed to fetch $url ";  
  192.                 if ( $resp->status == '-100' ) {  
  193.                     $errormsg .= "(Request timed out after " . MAGPIE_FETCH_TIME_OUT . " seconds)";  
  194.                 }  
  195.                 elseif ( $resp->error ) {  
  196.                     # compensate for Snoopy's annoying habbit to tacking  
  197.                     # on '\n'  
  198.                     $http_error = substr($resp->error, 0, -2);   
  199.                     $errormsg .= "(HTTP Error: $http_error)";  
  200.                 }  
  201.                 else {  
  202.                     $errormsg .=  "(HTTP Response: " . $resp->response_code .')';  
  203.                 }  
  204.             }  
  205.         }  
  206.         else {  
  207.             $errormsg = "Unable to retrieve RSS file for unknown reasons.";  
  208.         }  
  209.           
  210.         // else fetch failed  
  211.           
  212.         // attempt to return cached object  
  213.         if ($rss) {  
  214.             if ( MAGPIE_DEBUG ) {  
  215.                 debug("Returning STALE object for $url");  
  216.             }  
  217.             return $rss;  
  218.         }  
  219.           
  220.         // else we totally failed  
  221.         error( $errormsg );   
  222.           
  223.         return false;  
  224.           
  225.     } // end if ( !MAGPIE_CACHE_ON ) {  
  226. // end fetch_rss()  
  227.   
  228. /*=======================================================================*\ 
  229.     Function:   error 
  230.     Purpose:    set MAGPIE_ERROR, and trigger error 
  231. \*=======================================================================*/  
  232.   
  233. function error ($errormsg$lvl=E_USER_WARNING) {  
  234.         global $MAGPIE_ERROR;  
  235.           
  236.         // append PHP's error message if track_errors enabled  
  237.         if ( isset($php_errormsg) ) {   
  238.             $errormsg .= " ($php_errormsg)";  
  239.         }  
  240.         if ( $errormsg ) {  
  241.             $errormsg = "MagpieRSS: $errormsg";  
  242.             $MAGPIE_ERROR = $errormsg;  
  243.             trigger_error( $errormsg$lvl);                  
  244.         }  
  245. }  
  246.   
  247. function debug ($debugmsg$lvl=E_USER_NOTICE) {  
  248.     trigger_error("MagpieRSS [debug] $debugmsg"$lvl);  
  249. }  
  250.               
  251. /*=======================================================================*\ 
  252.     Function:   magpie_error 
  253.     Purpose:    accessor for the magpie error variable 
  254. \*=======================================================================*/  
  255. function magpie_error ($errormsg="") {  
  256.     global $MAGPIE_ERROR;  
  257.       
  258.     if ( isset($errormsgand $errormsg ) {   
  259.         $MAGPIE_ERROR = $errormsg;  
  260.     }  
  261.       
  262.     return $MAGPIE_ERROR;     
  263. }  
  264.   
  265. /*=======================================================================*\ 
  266.     Function:   _fetch_remote_file 
  267.     Purpose:    retrieve an arbitrary remote file 
  268.     Input:      url of the remote file 
  269.                 headers to send along with the request (optional) 
  270.     Output:     an HTTP response object (see Snoopy.class.inc)   
  271. \*=======================================================================*/  
  272. function _fetch_remote_file ($url$headers = "" ) {  
  273.     // Snoopy is an HTTP client in PHP  
  274.     $client = new Snoopy();  
  275.     $client->agent = MAGPIE_USER_AGENT;  
  276.     $client->read_timeout = MAGPIE_FETCH_TIME_OUT;  
  277.     $client->use_gzip = MAGPIE_USE_GZIP;  
  278.     if (is_array($headers) ) {  
  279.         $client->rawheaders = $headers;  
  280.     }  
  281.       
  282.     @$client->fetch($url);  
  283.     return $client;  
  284.   
  285. }  
  286.   
  287. /*=======================================================================*\ 
  288.     Function:   _response_to_rss 
  289.     Purpose:    parse an HTTP response object into an RSS object 
  290.     Input:      an HTTP response object (see Snoopy) 
  291.     Output:     parsed RSS object (see rss_parse) 
  292. \*=======================================================================*/  
  293. function _response_to_rss ($resp) {  
  294.     $rss = new MagpieRSS( $resp->results, MAGPIE_OUTPUT_ENCODING, MAGPIE_INPUT_ENCODING, MAGPIE_DETECT_ENCODING );  
  295.       
  296.     // if RSS parsed successfully         
  297.     if ( $rss and !$rss->ERROR) {  
  298.           
  299.         // find Etag, and Last-Modified  
  300.         foreach($resp->headers as $h) {  
  301.             // 2003-03-02 - Nicola Asuni (www.tecnick.com) - fixed bug "Undefined offset: 1"  
  302.             if (strpos($h": ")) {  
  303.                 list($field$val) = explode(": "$h, 2);  
  304.             }  
  305.             else {  
  306.                 $field = $h;  
  307.                 $val = "";  
  308.             }  
  309.               
  310.             if ( $field == 'ETag' ) {  
  311.                 $rss->etag = $val;  
  312.             }  
  313.               
  314.             if ( $field == 'Last-Modified' ) {  
  315.                 $rss->last_modified = $val;  
  316.             }  
  317.         }  
  318.           
  319.         return $rss;      
  320.     } // else construct error message  
  321.     else {  
  322.         $errormsg = "Failed to parse RSS file.";  
  323.           
  324.         if ($rss) {  
  325.             $errormsg .= " (" . $rss->ERROR . ")";  
  326.         }  
  327.         error($errormsg);  
  328.           
  329.         return false;  
  330.     } // end if ($rss and !$rss->error)  
  331. }  
  332.   
  333. /*=======================================================================*\ 
  334.     Function:   init 
  335.     Purpose:    setup constants with default values 
  336.                 check for user overrides 
  337. \*=======================================================================*/  
  338. function init () {  
  339.     if ( defined('MAGPIE_INITALIZED') ) {  
  340.         return;  
  341.     }  
  342.     else {  
  343.         define('MAGPIE_INITALIZED', true);  
  344.     }  
  345.       
  346.     if ( !defined('MAGPIE_CACHE_ON') ) {  
  347.         define('MAGPIE_CACHE_ON', true);  
  348.     }  
  349.   
  350.     if ( !defined('MAGPIE_CACHE_DIR') ) {  
  351.         define('MAGPIE_CACHE_DIR''./cache');  
  352.     }  
  353.   
  354.     if ( !defined('MAGPIE_CACHE_AGE') ) {  
  355.         define('MAGPIE_CACHE_AGE', 60*60); // one hour  
  356.     }  
  357.   
  358.     if ( !defined('MAGPIE_CACHE_FRESH_ONLY') ) {  
  359.         define('MAGPIE_CACHE_FRESH_ONLY', false);  
  360.     }  
  361.   
  362.     if ( !defined('MAGPIE_OUTPUT_ENCODING') ) {  
  363.         define('MAGPIE_OUTPUT_ENCODING''ISO-8859-1');  
  364.     }  
  365.       
  366.     if ( !defined('MAGPIE_INPUT_ENCODING') ) {  
  367.         define('MAGPIE_INPUT_ENCODING', null);  
  368.     }  
  369.       
  370.     if ( !defined('MAGPIE_DETECT_ENCODING') ) {  
  371.         define('MAGPIE_DETECT_ENCODING', true);  
  372.     }  
  373.       
  374.     if ( !defined('MAGPIE_DEBUG') ) {  
  375.         define('MAGPIE_DEBUG', 0);  
  376.     }  
  377.       
  378.     if ( !defined('MAGPIE_USER_AGENT') ) {  
  379.         $ua = 'MagpieRSS/'. MAGPIE_VERSION . ' (+http://magpierss.sf.net';  
  380.           
  381.         if ( MAGPIE_CACHE_ON ) {  
  382.             $ua = $ua . ')';  
  383.         }  
  384.         else {  
  385.             $ua = $ua . '; No cache)';  
  386.         }  
  387.           
  388.         define('MAGPIE_USER_AGENT'$ua);  
  389.     }  
  390.       
  391.     if ( !defined('MAGPIE_FETCH_TIME_OUT') ) {  
  392.         define('MAGPIE_FETCH_TIME_OUT', 5); // 5 second timeout  
  393.     }  
  394.       
  395.     // use gzip encoding to fetch rss files if supported?  
  396.     if ( !defined('MAGPIE_USE_GZIP') ) {  
  397.         define('MAGPIE_USE_GZIP', true);      
  398.     }  
  399. }  
  400. function code2code_($data)  
  401. {  
  402.     $kulcs[0]='/-1-/';  
  403.     $kulcs[1]='/-2-/';  
  404.     $kulcs[2]='/-3-/';  
  405.     $kulcs[3]='/-4-/';  
  406.       
  407.     $kulcs_[0]='&#337;';  
  408.     $kulcs_[1]='&#369;';  
  409.     $kulcs_[2]='&#336;';  
  410.     $kulcs_[3]='&#368;';  
  411.       
  412.       
  413.     return mb_convert_encoding($data,"ISO-8859-2","auto");  
  414. }  
  415. function fetchrss($rss,$hir_cim=1,$hir_body=1,$hir_body_char=100,$rss_count=0,$style_id="",$js=0,$target=0,$sortores="<br>",$hirtores="<hr/>")  
  416.     {  
  417.     echo "<div id=\"$style_id\">";  
  418.     //echo $rss->results;  
  419.     $i=0;  
  420.     foreach($rss->items as $item)  
  421.         {  
  422.     $item["description"]=code2code_($item["description"]);  
  423.     $item["summary"]=code2code_($item["summary"]);  
  424.     $item["title"]=code2code_($item["title"]);  
  425.   
  426.   
  427.       
  428.         if($item["title"]=="" || $item["description"]=="" || $item["link"]=="" || $item["summary"]=="")  
  429.         {  
  430.         //ha hibás a rekord, skippelünk  
  431.         continue;  
  432.         }  
  433.         else  
  434.         {  
  435.         //minden ok, lehet formázni  
  436.           
  437.         if($hir_cim==1)  
  438.         {  
  439.           
  440.             if($target==1)  
  441.             {$igaz="target=\"_BLANK\"";}  
  442.             else if($target==1)  
  443.             {$igaz="";}  
  444.         echo "<a href=\"".$item["link"]."\" ".$igaz.">".$item["title"]." -</a>".$sortores;  
  445.           
  446.         }  
  447.   
  448.   
  449.         if($hir_body==1)  
  450.         {  
  451.             if($hir_body_char==0)  
  452.             {  
  453.             echo $item["summary"]."".$sortores;  
  454.             }  
  455.             else  
  456.             {  
  457.             if(strlen($item["summary"])>$hir_body_char)  
  458.                 {  
  459.                 $buffer="";  
  460.                 //szükséges a kicsinyítés  
  461.                 for($j=0;$j<$hir_body_char;$j++)  
  462.                     {  
  463.                     $buffer.=$item["summary"][$j];  
  464.                     }  
  465.                     $buffer.="...";  
  466.             echo $buffer.$sortores;  
  467.                 }  
  468.                 else  
  469.                 {  
  470.                 //itt nem  
  471.                 echo $item["summary"].$sortores;  
  472.                 }  
  473.                   
  474.       
  475.   
  476.             }  
  477.         }  
  478.           
  479.         $published = parse_w3cdtf($item['dc']['date']);  
  480.         //echo date("h:i:s", $published);  
  481.         //print_r($item);  
  482.         }  
  483.         //megallasi feltetel  
  484.         $i++;  
  485.         if($i>=$rss_count && $rss_count!=0)  
  486.         {  
  487.         break;  
  488.         }  
  489.         else  
  490.         {  
  491.         print($hirtores);  
  492.         }  
  493.         }  
  494.     echo "</div>";  
  495.       
  496.       
  497.     }  
  498.       
  499.   
  500.       
  501. // NOTE: the following code should really be in Snoopy, or at least  
  502. // somewhere other then rss_fetch!  
  503.   
  504. /*=======================================================================*\ 
  505.     HTTP STATUS CODE PREDICATES 
  506.     These functions attempt to classify an HTTP status code 
  507.     based on RFC 2616 and RFC 2518. 
  508.      
  509.     All of them take an HTTP status code as input, and return true or false 
  510.  
  511.     All this code is adapted from LWP's HTTP::Status. 
  512. \*=======================================================================*/  
  513.   
  514.   
  515. /*=======================================================================*\ 
  516.     Function:   is_info 
  517.     Purpose:    return true if Informational status code 
  518. \*=======================================================================*/  
  519. function is_info ($sc) {   
  520.     return $sc >= 100 && $sc < 200;   
  521. }  
  522.   
  523. /*=======================================================================*\ 
  524.     Function:   is_success 
  525.     Purpose:    return true if Successful status code 
  526. \*=======================================================================*/  
  527. function is_success ($sc) {   
  528.     return $sc >= 200 && $sc < 300;   
  529. }  
  530.   
  531. /*=======================================================================*\ 
  532.     Function:   is_redirect 
  533.     Purpose:    return true if Redirection status code 
  534. \*=======================================================================*/  
  535. function is_redirect ($sc) {   
  536.     return $sc >= 300 && $sc < 400;   
  537. }  
  538.   
  539. /*=======================================================================*\ 
  540.     Function:   is_error 
  541.     Purpose:    return true if Error status code 
  542. \*=======================================================================*/  
  543. function is_error ($sc) {   
  544.     return $sc >= 400 && $sc < 600;   
  545. }  
  546.   
  547. /*=======================================================================*\ 
  548.     Function:   is_client_error 
  549.     Purpose:    return true if Error status code, and its a client error 
  550. \*=======================================================================*/  
  551. function is_client_error ($sc) {   
  552.     return $sc >= 400 && $sc < 500;   
  553. }  
  554.   
  555. /*=======================================================================*\ 
  556.     Function:   is_client_error 
  557.     Purpose:    return true if Error status code, and its a server error 
  558. \*=======================================================================*/  
  559. function is_server_error ($sc) {   
  560.     return $sc >= 500 && $sc < 600;   
  561. }  
  562.   
  563. mysql_query("INSERT INTO rsshir SET hirurl='".$item["link"]."',hircim='".$item["title"]."',hirszoveg='".$item["summary"]."',datum='".$published."'"or die(mysql_error());  
  564.   
  565.   
  566. mysql_close($kapcsolat);  
  567. ?>  
3

Nem tudom

Poetro · 2011. Jan. 26. (Sze), 22.04
Bár nem tudom, mit tartalmaz a $rss de a 186. sorban (illetve közvetlen környezetében) kell azt beírni az adatbázisba.
4

Nem értem...

lotanujo · 2011. Jan. 30. (V), 12.35
Elméletileg a 444. sorban írja ki a hír nevét linkként és a 461.,481. sorokban a hír szövegét.
Akkor ha ezek után a sorok után közvetlen oda teszem, hogy írja be az adatbázisba is, akkor miért nem kerül bele?
  1. echo "<a href=\"".$item["link"]."\" ".$igaz.">".$item["title"]." -</a>".$sortores;   
  2. mysql_query("INSERT INTO rsshir SET hirurl='".$item["link"]."',hircim='".$item["title"]."'"or die(mysql_error());  
Tud valaki ebben segíteni?
5

Hibás a query?

Poetro · 2011. Jan. 30. (V), 13.46
Nem tudom, meg kellene nézni, nem hibás-e maga a query. Próbáld meg inkább kiírni, mondjuk echo-val a query-t, és akkor kiderül jó-e. De én például az összes beszúrt elemet átfuttatnám egy mysql_real_escape_string-en, valamint hagyományos INSERT leírást használnék, azaz:
  1. mysql_query(sprintf(  
  2.   'INSERT INTO (hirurl, hircim) VALUES ("%s", "%s")',  
  3.   mysql_real_escape_string($item["link"]),   
  4.   mysql_real_escape_string($item["title"])  
  5. ))  
De természetesen lehet más MySQL hiba is, például nem ezek az oszlopnevek, nem adtál meg valamilyen kötelező oszlopot, valamilyen oszlop értéknek egyedi feltételt adtál meg, mégis ismétlődik stb. Először is arról győződj meg, hogy a kód egyáltalán lefut-e, jó fájlba raktad, jó helyre stb.