Search code examples
phpropencpu

how to have R opencpu interact with php


I am new to php and have some experience with R opencpu.

Suppose I use localhost (127.0.0.1) to host my opencpu with the (default) port of 8004. Then, I cannot run the Apache using the same port. Instead, I have to run it in another port, say 8080. Then all my .php will be held on a server of the port 8080.

So how can I connect my .php files when running a opencpu app?

I thought that I could put my .php files in inst/www, where my index.html is. However, when I do so and open the page of http://localhost/ocpu/library/.../www/xxx.php, my browser automatically downloads the .php file but not executing it.


Solution

  • You'll need to ensure your web server is configured to properly serve PHP, which is a whole other thing. However, if you're only accessing files on localhost then you can just execute the PHP files directly using the system2() command.

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

    Setting stdout=TRUE ensures the output from the script is returned for later use.