如何让 R opencpu 与 php 交互

how to have R opencpu interact with php

我是 php 的新手,对 R opencpu 有一些经验。

假设我使用 localhost (127.0.0.1) 以(默认)端口 8004 托管我的 opencpu。然后,我不能 运行 Apache 使用相同的端口。相反,我必须 运行 它在另一个端口,比如 8080。然后我所有的 .php 将保存在端口 8080 的服务器上。

那么,当 运行 使用 opencpu 应用程序时,如何连接我的 .php 文件?

我认为我可以将我的 .php 文件放在我的 index.html 所在的 inst/www 中。但是,当我这样做并打开 http://localhost/ocpu/library/.../www/xxx.php 的页面时,我的浏览器会自动下载 .php 文件但不会执行它。

您需要确保您的 Web 服务器配置为正确服务 PHP,这是一个 whole other thing. However, if you're only accessing files on localhost then you can just execute the PHP files directly using the system2() 命令。

output <- system2('php', c('/path/to/script.php', 'arg1', 'arg2'), stdout=TRUE)

设置stdout=TRUE确保返回脚本的输出供以后使用。