tags:

views:

86

answers:

4

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!

A: 

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.

Koraktor
+3  A: 

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.

vartec
Would Ruby do this?
Andrei Serdeliuc
+1  A: 

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.

nlaq
I assume I couldn't just dive into that with C++, but if / when I get to the point where I'm fairly happy with it, what should I look for when approaching a problem like this one? Or will that come to me whilst learning C++?
Andrei Serdeliuc
A: 

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.

Ólafur Waage
I don't think he tries to make WUI, in the question he states "I'm trying to go beyond web dev"
vartec