开发者

how to call ruby script from php?

I am writing a Wordpress plug in php in and next step is some kind of add on to thi开发者_JAVA技巧s plug in.

The add on would scrape data from web, sending forms etc. I have this part almost ready from the time before I had any thoughts about Wordpress plugin - it's coded in ruby using mechanize. I haven't found anything similar to mechanize in php anyway.

But I do not know what is the best way to call my ruby script from Wordpress. Some tasks will be managed by cron. What about the ones based on user request?

  • php script only triggers the ruby script. It won't wait/require anything from ruby's output
  • Wordpress plugin is fully portable and functional without ruby script. Ruby adds on something more. If somebody requires it.
  • everything will be running on my linux server where I have a root access


A WordPress plugin that depends on Ruby isn't going to be portable. That's OK if you're the only one who will be using it, though.

If the Ruby script needs to return a result that will be used immediately by the PHP script that's calling it, then something like exec() is the only way. Make sure you escape any arguments you pass to the Ruby script; otherwise you'll be vulnerable to injection attacks.

If the Ruby script doesn't need to return a result immediately (e.g. some background processing, such as thumbnail generation) then I think the best way would be for the PHP script to insert a row into a MySQL database or something similar. The Ruby script can work in the background or run from cron, check the database periodically for new jobs, and do whatever processing it needs to do. This approach avoids the performance overhead and security issues of exec(), and it's arguably also more scalable. (A similar approach would have the Ruby script listen on a socket, and your PHP scripts would connect to the socket. But this requires more work to get it right.)


If i were you i would handle all the ruby stuff from the cron. Make a queue in the DB to hand user requests then make the script (in ruby?) invoked by cron grab all the unprocessed jobs from the queue and start running them, then remove the job from the queue (or set some kind of flag for it being done). This way you dont have to call exec which in most cases is going to be off limits unless the user is running on VPS/Dedicated server where they have root access.

You could also make this a seperate job and have it poll the DB for unprocessed jobs more regularly than the primary job... if necessary.

Still, this begs the question... why use ruby in a php blog/cms app??????


Use exec() to run the ruby interpreter, giving it the path to your ruby script.

http://php.net/manual/en/function.exec.php

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜