如何从php调用ruby脚本? [英] how to call ruby script from php?

查看:224
本文介绍了如何从php调用ruby脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用php编写Wordpress插件,下一步是对该插件进行某种添加.

I am writing a Wordpress plug in php in and next step is some kind of add on to this plug in.

该插件会从Web上抓取数据,发送表单等.在我对Wordpress插件有任何想法之前,我几乎已经准备好这部分-它是在 ruby​​中使用机械化编码的.无论如何,我还没有在php中找到任何类似于机械化的东西.

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.

但是我不知道从Wordpress调用我的ruby脚本的最佳方法是什么.有些任务将由cron管理.基于用户请求的那些呢?

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脚本仅触发红宝石脚本.它不会等待/不需要ruby的输出中的任何内容
  • Wordpress插件是完全可移植且功能强大的没有ruby 脚本. Ruby添加了更多内容.如果有人需要.
  • 所有内容都将在我具有root访问权限的linux服务器上运行
  • 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

推荐答案

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

这篇关于如何从php调用ruby脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆