douxiangdiao6348 2010-07-04 17:43
浏览 102
已采纳

通信,在PHP App和Ruby App之间传递信息

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

  • 写回答

2条回答 默认 最新

  • 普通网友 2010-07-04 18:21
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c