views:

33

answers:

2

I primarily work in PHP and prefer to do so since there seem to be more jobs in this language, at least in my area (and I'm still fairly new to it so I want to continue to learn the language better).. but for some things I want to do I need to use the WWW Mechanize library that doesn't work with PHP but does with Ruby (yes I know PHP has some alternatives but I have tried them and they don't work for me so I need to do this), so I'd like to write most of my app in PHP and then just call Ruby when I need to use this library, then pass the info back to PHP, yes I know this would be "slow" but thats not an issue in this case as this isn't a public web app, its just for business use..

I'm wondering what the best way would be to pass info between the 2 languages.. I have thought of using http POST (like with Curl in PHP) to do this but not sure if this is the most efficient way any.. any info is appreciated, thanks

+1  A: 

I would suggest following a Software as a Service Architectire (SOA) and running a Ruby/Rails application as a separate process. You'll have to develop an API between the two (a very simple one will work): using POST/GET as you first thought is a right way to go here.

Zepplock
Thanks, I'll look into SOA.. I'm just using Ruby, not Rails since I don't need a framework for this on the ruby side as I think it would only complicate using the 3rd party library, unless it supports it natively, so I'll have to look into that
Rick
I would suggest using Rails (which is not 3rd party by the way), which will give a nice model/view/controller framework and HTTP bindings, since your API will be HTTP based. You can also look into RESTful APIS. Which will simplify a lot of things (CRUD like style of API)
Zepplock
I mean the library is 3rd party, not rails
Rick
+1  A: 

There are two different ways that I would do this:

\1. In ruby, set up a non-HTTP server that only listens on '::' (or 127.0.0.1 if you don't like ipv6). Then, every time your PHP script needs to do something, it can connect to the server and pass data to it. This would be the fastest solution because the ruby script doesn't need to start up every time PHP needs to do something.

Example 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

Example Ruby client: (you will need to translate this to 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

You could use process watching tools like http://god.rubyforge.org/ to keep the server running.

\2. Make the ruby script a command line utility, and use exec in PHP to call it.

An example command line script:

require 'mechanize'

def do_mechanize_stuff(command, *args)
  # ... from previous example
end

do_mechanize_stuff ARGV.shift, ARGV
Adrian
thanks, I'll try that
Rick