How to call ruby ​​script from php?

I am writing Wordpress in php, and the next step is something like adding to this plugin.

Adding will clear data from the Internet, submit forms, etc. I have this part, almost ready from that time, before I had any thoughts about the Wordpress plugin - it was encoded in ruby using mechanization . In any case, I did not find anything like mechanization in php.

But I do not know how best to call my ruby ​​script from Wordpress . Some tasks will be performed by cron. What about those based on user request?

  • php script only runs ruby script. He will not wait / demand from a ruby ​​withdrawal
  • The Wordpress plugin is fully portable and functional without a ruby ​​script . Rubin adds something else. If someone requires this.
  • everything will work on my linux server where i have root access
+4
source share
3 answers

A WordPress plugin that depends on Ruby will not be portable. This is normal if you are the only one who will use it.

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

If the Ruby script does not need to immediately return the result (for example, some background processing, for example thumbnail generation), then I think the best way for a PHP script is to insert a string into a MySQL database or something like that. Ruby script can run in the background or run from cron, periodically check the database for new tasks and perform any necessary processing. This approach avoids the performance and security issues of exec (), and possibly is also more scalable. (A similar approach would make Ruby script listen on the socket, and your PHP scripts would connect to the socket, but this requires more work to get everything right.)

+6
source

If I were you, I would handle all ruby ​​things from cron. Make a queue in the database for sending user requests, then create a script (in ruby?), Called cron, grab all the unprocessed jobs from the queue and start them, then remove the job from the queue (or set some type of flag for this to be done). Thus, you do not need to call exec , which in most cases will be disabled if the user does not work on the VPS / dedicated server, where they have root access.

You can also do this separate task and test the database for unprocessed tasks more regularly than the main task ... if necessary.

However, this asks the question ... why use ruby ​​in php blog / cms app ??????

+2
source

Use exec () to start the ruby ​​interpreter by giving it the path to your ruby ​​script.

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

+1
source

All Articles