dounao1875 2013-09-18 13:17
浏览 61
已采纳

php脚本加载txt文件并生成rss - cron?

I would like to make a script that gets lines from txt file and makes new rss feed item for every line. Txt file will be frequently updated with new lines of data, so the script should check every hour if there is a new line, and if there is, needs to make new feed item.

I found this txt2rss script, but I'm not so proficient and there is little documentation on how to modify and use it. http://users.ninthfloor.org/~ashawley/txt2rss/txt2rss.php.html

Could someone point me in the right direction? PHP with cronjob? Or maybe there is a simplier way?

Appreciate it

P.S:. I also found this script.

public static function create($info, $items, $format = 'rss2', $encoding = 'UTF-8') 
{ 
    $info += array('title' => 'Generated Feed', 'link' => '', 'generator' => 'KohanaPHP'); 

    $feed = '<?xml version="1.0" encoding="'.$encoding.'"?><rss version="2.0"><channel></channel></rss>'; 
    $feed = simplexml_load_string($feed); 

    foreach ($info as $name => $value) 
    { 
        if ($name === 'image') 
        { 
            // Create an image element 
            $image = $feed->channel->addChild('image'); 

            if ( ! isset($value['link'], $value['url'], $value['title'])) 
            { 
                throw new Kohana_Exception('Feed images require a link, url, and title'); 
            } 

            if (strpos($value['link'], '://') === FALSE) 
            { 
                // Convert URIs to URLs 
                $value['link'] = URL::site($value['link'], 'http'); 
            } 

            if (strpos($value['url'], '://') === FALSE) 
            { 
                // Convert URIs to URLs 
                $value['url'] = URL::site($value['url'], 'http'); 
            } 

            // Create the image elements 
            $image->addChild('link', $value['link']); 
            $image->addChild('url', $value['url']); 
            $image->addChild('title', $value['title']); 
        } 
        else 
        { 
            if (($name === 'pubDate' OR $name === 'lastBuildDate') AND (is_int($value) OR ctype_digit($value))) 
            { 
                // Convert timestamps to RFC 822 formatted dates 
                $value = date('r', $value); 
            } 
            elseif (($name === 'link' OR $name === 'docs') AND strpos($value, '://') === FALSE) 
            { 
                // Convert URIs to URLs 
                $value = URL::site($value, 'http'); 
            } 

            // Add the info to the channel 
            $feed->channel->addChild($name, $value); 
        } 
    } 

    foreach ($items as $item) 
    { 
        // Add the item to the channel 
        $row = $feed->channel->addChild('item'); 

        foreach ($item as $name => $value) 
        { 
            if ($name === 'pubDate' AND (is_int($value) OR ctype_digit($value))) 
            { 
                // Convert timestamps to RFC 822 formatted dates 
                $value = date('r', $value); 
            } 
            elseif (($name === 'link' OR $name === 'guid') AND strpos($value, '://') === FALSE) 
            { 
                // Convert URIs to URLs 
                $value = URL::site($value, 'http'); 
            } 

            // Add the info to the row 
            $row->addChild($name, $value); 
        } 
    } 

    if (function_exists('dom_import_simplexml')) 
    { 
        // Convert the feed object to a DOM object 
        $feed = dom_import_simplexml($feed)->ownerDocument; 

        // DOM generates more readable XML 
        $feed->formatOutput = TRUE; 

        // Export the document as XML 
        $feed = $feed->saveXML(); 
    } 
    else 
    { 
        // Export the document as XML 
        $feed = $feed->asXML(); 
    } 

    return $feed; 
}
  • 写回答

1条回答 默认 最新

  • doufuxi7093 2013-09-18 13:34
    关注

    In your crontab file:

    59 * * * * /path/to/php -f /path/to/my/text/to/rss/file.php
    

    If you want to be notified every time it runs do the following:

    59 * * * * /path/to/php -f /path/to/my/text/to/rss/file.php 2>&1 | mailx -s "Ran Text to RSS Job" youremail@whatever.com
    

    This will email you when the job has completed with a subject "Ran Text to RSS Job," if there was an error during execution it will be the body of the email otherwise the message will be blank.

    This will run at the 59th minute of every hour.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于这个复杂问题的解释说明
  • ¥50 三种调度算法报错 采用的你的方案
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败