Running PHP Scripts in R - php

There's a PHP library that I'm really interested in using. However, there's no R implementation of it nor do I see an interpreter between the two. I've searched on Google but it seems like most implementations are either displaying R plots or figures on a webpage via PHP or passing command-line arguments to each script. R blogger article about PHP and R seemed to be geared towards building a web application.
I've seen reticulate package achieve similar this within Python, but I was hoping we have something like this for PHP.
I've thought of a method of passing it through via exec(paste0('php ', <script>), but I don't think that's the best direction to take.
Are there better ways to implement this?

Related

Call CPLEX from PHP

I need to make my PHP app cooperate with CPLEX Solver. The good news is I'm familiar with LP, but the bad news is I haven't used CPLEX until now. From the documentation I can see that using CPLEX is fairly easy through the integrated IDE, or via default C, JAVA, C#, Python APIs or a callable library API. Unfortunately, I can't seem to find any examples at all of CPLEX being used from PHP.
Is such a thing even possible? I know I can execute any bash command through exec, but CPLEX often requires several interactive steps to be performed to solve a single problem, and I don'g know how to do that from PHP;
If answer to 1. is 'yes', can anyone share even a simple example of how it can be done? I.e. a PHP script that calls CPLEX to resolve an LP problem.
There won't likely be any ready made examples because there is only a callable lib for C and an API for C++, Java and C#. There is also a separate Python API.
You could potentially use Gearman or write a REST wrapper in Python that your PHP app could consume.
Alternatively you could write your own PHP extension that wraps the C functions you need to call. Not sure if you can precisely what you need to do with Zephir but you could have a look - it's really simple to get started with...

Connecting C with php/asp

I am learning to build a web application based on graph theory(Linked-In) kinda thing. So as I thought of considering PHP for getting the inputs from user via web page and using C for internal programming like adding,deleting vertex, edge to/from the graph with the help of C structures and various algorithms. So I need to know how to use C code to drive my functions while I use ASP or PHP for handling webpage.
I wish to use C preferably.
Using Xampp 1.7.4 on Windows7
Actually it shouldn't be that hard to code...
If you already learned C, Then I suggest you to start learning sockets.
You didn't specify the OS, So here's links to a great tutorials both
Linux
&
Windows.
Now, if you don't want to use sockets, I suggest you to take a look at "Apache - Thrift".
You can communicate between functions in PHP and functions in C.
Example -
$shutdown->Shutdown();
Or
$points = (int*)points;
Good luck.

Extend PHP to communicate with R programming language

I'm on a road to build a PHP extension module to allow PHP to communicate with the R programming language.
My objective for this module is to allow me to run some R function directly from PHP.
I haven't build any extension on PHP before, but I know how to write a php program. I know a little bit about C and C++
I'm interested to hear what the expert advice on this one.
Here's some of the questions I want to know:
Is it possible to achieve the above idea?
What tools do I need?
What is the effective way to do this?
Please use the search feature at the top-right before you post.
Similar questions have been asked before, see e.g.
Pass and get variables between PHP and R
Integration of R Language with php to take the result from R
php and R integration
and there are probably more.

How would I go about expanding my PHP code library with Python code?

I have a rather large API written in PHP that I've worked on for years. Some of the functionality needs to be upgraded to the extent where I should really just rewrite the classes. Since I need to scrap the old code anyway, I was thinking perhaps I could replace the old functionality with Python code.
I came across PiP while searching for answers, and it seems like an exellent solution (Since I can actually create Python class instances in PHP and call their methods etc.) but it seems it was abandoned in the Alpha stages for reasons unknown to me.
I suppose the simplest solution would be a CLI one, meaning I could run a python instance from PHP and collect the results. I don't particularly like this solution though, considering the amount of Python code I'd have to write just to handle input from PHP and respond accordingly. Plus it's not very reusable.
I don't know if this is a normal problem, Google certainly don't seem to think so, but what would be the best way of complementing a PHP code library with Python code?
I can think of two options, which should also make the overall solution somewhat more flexible:
Write it as a web service: Write the python parts as a RESTful web service. Should be relatively straightforward to do.
Use something like ZeroMQ to create a message queue: There are zmq libraries for both PHP and Python, which should also make this option not very difficult to implement.
As you may have noticed, all the options can be somewhat clunky in nature. You didn't mention what exactly did you mean by "API" (eg. is it a library or web service or what), so depending on what it is, it might just be easiest to rewrite it in PHP - it's not a bad language in the end if used properly.
After some time researching this, I've concluded that there is no worthwhile way of building on a PHP library with Python code. The PiP project was the closest thing I could find but it has been abandoned.
My solution has been to declare a 2.0. Every new function I add I write in Python. I translate some code from PHP to Python when necessary. It's working great so far!

PHP complex uses - Interact with external devices

Is it possible to access & control external devices in PHP?
For example, would it be capable of changing the speed of a USB fan or change the direction of a wireless toy car.
I'm trying to go beyond web dev and I'm wondering if the programming language I use is capable of handling my ideas or should I consider changing the environment.
If these would be possible, I'd very much appreciate any pointers to reading materials or suggestions on other languages that might be more suitable.
Thanks!
On Linux it surely is possible by accessing /dev/ files. But it'll be very tedious. I'd recommend you switching to Python, Ruby, Lua or Java.
For example there are bindings for libusb for Python, Ruby, Lua and Java.
You could write an external program, then use PHP's exec (or was it system?) function to interact with the executable or script.
Seems like the most sane way to do it. Another good alternative is to build a program or script that controls an external device that can communicate with a RESTfull type API exposed via HTTP - and then use lib_curl in PHP land to talk back and forth between it. Believe me, building a basic HTTP server in C++ that can be used to be remote controlled with PHP (or JS for that matter) is very simple.
Wait
I think I read the question wrong ;)
If you want to get into really cool stuff, I say that you learn C++. C++ is a great language that not only opens a lot of doors, but also provides a good learning experience. C++ is lots and lots of fun.
In response to comment
In the case with USB its a bit different and more complicated (as USB has an established protocol and such) but serial is as easy as dumping data into a handle.
You should be able to pick up C++ to get to that point fairly soon. Either way it's a great experience.
This would be possible with a extension, but not with pure PHP code. I don't know of any extensions being able to do something like this, but I think it should be possible.
If you find a command line tool that does this, then you can control it using exec(), system(), passthru() or shell_exec() (based on what output the program gives you back)
Just be sure to escapeshellcmd() if you give access to this program from a public website.

Categories