php+ruby(with ActiveRecord)又一新範例 -- RSS聯撥器

本文同步發佈至:
[url]http://blog.pixnet.net/zusocfc/post/4563945[/url]
[url]http://zusocfc.blogspot.com/2007/05/phprubywith-activerecord-rss.html[/url]

上個例子,我們用PHP + Ruby 搭配ActiveRecord的方式來寫資料新增的程式
今天我們就來延伸應用一下,要做什麼呢? RSS聯撥器!
有鑒於GoogleReader的RSS聯撥器產生出來的東西太醜(只能修改一兩個小地方.. 我總覺得那好胖= =),乾脆自己寫個來用
Demo網址改天再PO上來,我們先來寫程式比較重要:P
主機請記得先裝好Ruby、PHP、Apache跟MySQL;OS要啥都沒差,我比較建議LAMP的配置XD
我們來建立一個叫做feeds的目錄包含一個子目錄,叫做lib:

[quote]mkdir -p feeds/lib[/quote]


先跳到feeds/lib新增幾個會被require的檔案:

[quote]
cd feeds/lib
touch connect.rb model.rb require.rb
[/quote]


以下是各個檔案的用處:

[quote] - connect.rb
資料庫連線初始化
- model.rb
資料表模型宣告
- rqeuire.rb
會用到的額外library引入
[/quote]

原始碼:
[code]- connect.rb
#!/usr/bin/env ruby;require 'lib/require';ActiveRecord::Base.establish_connection({:adapter => "mysql",:host => "localhost",:username => "username",:password => "password",:database => "others"})
- model.rb
#!/usr/bin/env ruby;require 'lib/connect';class Feed < ActiveRecord::Base;end
- require.rb
#!/usr/bin/env ruby;%w|rubygems active_record hpricot open-uri|.each{|lib| require lib}
[/code]

一切搞定後,我們可以開始來建立資料庫了!

[quote] mysql> create database others;
mysql> use others;
mysql> create table feeds(id int, uri varchar(255));
mysql> describe feeds;[/quote]


看看資料表結構是否正確!
接著回到上一層目錄,新增底下的幾個檔案:
[quote]
touch index.php list.rb new.htm new_record.rb save.php


- index.php
網站首頁,會列出目前的RSS feed
- save.php
儲存RSS feed網址
- list.rb
處理RSS feed
- new_record.rb
將RSS feed網址存入資料庫(也可以直接用php寫.. 我是沒有意見)
- new.htm
新增RSS feed網址的表單[/quote]


原始碼我就直接貼了

[code] - index.php
<html>
<head>
<title></title>
</head>
<body>
<a href="new.htm">Create</a>
<ul>
<?php
exec("ruby list.rb", $args);
for($i=0;$i<count($args);$i+=3)
echo "<li><a href=\"" . $args[$i+1] . "\" title=\"作者:" . $args[$i+2] . "\">" . $args[$i] . "</a> -- " . $args[$i+2] . "</li>";
?>
</ul>
</body>
</html>


- save.php
<?php
exec("ruby new_record.rb " . $_POST["feed_uri"], $arg);
if ($arg) echo "<script>location.href=\"index.php\";</script>";
?>



- list.rb
#!/usr/bin/env ruby
=begin
Filename: list.rb
=end
require 'lib/model'

Feed.find(:all).each{|feed|
doc = Hpricot(open(feed.uri))
rss = doc.search("entry")
max = rss.size > 3 ? 3 : rss.size
max.times {|i|
break if rss.nil?
puts rss[i].search("title").text.gsub(/\n/, " ") # Return the title of the article to the PHP file.
puts rss[i].search("link[@rel='alternate']")[0]["href"].gsub(/\n/, " ") # Return the link of the article to the PHP file.
puts rss[i].search("author/name").text.gsub(/\n/, " ") # Return the author of the article to the PHP file.
}
}


- new_record.rb
#!/usr/bin/env ruby
=begin
Filename: new_record.rb
=end
require 'lib/model';puts Feed.new({:uri => ARGV[0]}).save



- new.htm
<html>
<head>
<title></title>
</head>
<body>
<form action="save.php" method="post">
<p>Please input the feed url:<input type="text" name="feed_uri" /></p>
<p><input type="submit" value="Save!" /></p>
</form>
</body>
</html>[/code]

OK,這樣就可以啦XD
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值