且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从php调用ruby脚本?

更新时间:2023-12-05 13:43:16

依赖Ruby的WordPress插件将无法移植.不过,如果您是唯一使用它的人,那就没关系.

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.

如果Ruby脚本需要返回一个将由调用它的PHP脚本立即使用的结果,则类似于

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.

如果Ruby脚本不需要立即返回结果(例如某些后台处理,例如缩略图生成),那么我认为***的方法是让PHP脚本在MySQL数据库或类似的数据库中插入一行. Ruby脚本可以在后台运行,也可以在cron中运行,定期检查数据库中是否有新作业,并执行所需的任何处理.这种方法避免了exec()的性能开销和安全性问题,并且可以说它还具有更高的可伸缩性. (一种类似的方法是让Ruby脚本在套接字上侦听,而您的PHP脚本将连接到该套接字.但这需要更多的工作才能正确完成.)

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.)