有两种不同的方式,我会这样做:
\ 1。在ruby中,建立一个只侦听'::'(或127.0.0.1,如果你不喜欢ipv6)的非HTTP服务器。然后,每当您的PHP脚本需要做某件事时,它都可以连接到服务器并将数据传递给它。这将是最快的解决方案,因为每次PHP需要做某件事时,ruby脚本都不需要启动。
例红宝石:
require 'mechanize'
require 'socket'
def do_mechanize_stuff(command, *args)
case command
when 'search_google'
# search google with args.join(' ')
when 'answer_questions_on_stackoverflow'
# answer questions on stackoverflow
# with mechanize
end
'the result to pass to PHP'
end
srv = TCPServer.new '::', 3000
loop do
Thread.new(srv.accept) do |sock|
sock.write(
do_mechanize_stuff *sock.gets.split(' ')
)
sock.close
end
end
例红宝石客户端:(你需要这个翻译成PHP)
require 'socket'
# This is a script that searches google
# and writes the results to stdout.
s = TCPSocket.new 'localhost', 3000
s.puts 'search_google how to use a keyboard'
until (r = s.gets).nil?
print r # a search result.
end
你可以使用像http://god.rubyforge.org/过程看工具,保持服务器运行。
\ 2。使ruby脚本成为一个命令行工具,并使用PHP中的exec
来调用它。
一个示例命令行脚本:
require 'mechanize'
def do_mechanize_stuff(command, *args)
# ... from previous example
end
do_mechanize_stuff ARGV.shift, ARGV
感谢,我会尝试 – Rick 2010-07-04 18:46:53