link PHP with Octave or Matlab - php

Suppose I have a lot of math calculations which are quite tedious to implement in php. Is it possible to somehow link PHP and Octave on the server in such a way, that php sends parameters to Octave and receives answers back.
Has anyone tried anything similar?

Another solution is to use octave-daemon, which was written specifically for this purpose. Works on Linux, don't know about Windows.

You can use matlab compiler to make an executable matlab application, that you can call from php.

GNU Octave can be called from PHP using the Linux command line, using commands like exec() or passthru(). Anyway, their appropriate use depends on what you are trying to do (there are no details of your problem).

One way to do it, in Windows, is to compile Matlab as a DLL and include it ona web app (a WFC service, for example). At that point you have a functional "matlab service" and then you can access that service from PHP or any other language.

It is also possible to create a .NET component using Matlab Builder NE, and deploy it using SilverLight on the web.

Related

MATLAB with PHP [duplicate]

As the title indicates, I have MATLAB code for isolated spoken words recognition, and I want to be able to integrate this project with another one made with PHP for some purpose.
I have not used to deal with such problem before. In other words, it's the first time for me when I need to integrate PHP and MATLAB, so I really don't know where to start and how.
I have read a couple of articles, but I couldn't make it valid.
I have PHP 5.4.9, MATLAB R2012A and Windows 7.
The MATLAB project files can be seen on GitHub.
You have a few options here:
If MATLAB installed on the server where the PHP application would be deployed (not your current development environment), you can invoke it directly just like any other program (matlab -r "...") using whatever is the equivalent of EXECUTE command in PHP. Here are some resources (make sure to also checkout the linked questions as well):
How to call MATLAB from command-line and print to stdout before exiting
Running a cmd file without GUI popping up
Pass Parameters _ Shell Script - Octave Script
Others have commented on how to pass input/output between PHP and your MATLAB script. For example, you could design your MATLAB function to receive the path of WAV file as input, process it and save any resulting image to disk:
function myFunc(filename)
[y,Fs] = audioread(filename);
img = my_process_func(y, FS);
imwrite(img, 'out.png');
end
Which is invoked from PHP as:
% Of course you have to make sure "myFunc" is available on the MATLAB path.
% Think: "addpath(..)" or just "cd(..)" into the directory first
matlab -wait -nodisplay -r "myFunc('audio.wav'); quit;"
You could then read the output image in the PHP application.
If not, what deployment-related toolboxes do you have available? MATLAB Compiler and related toolboxes like MATLAB Builder NE and MATLAB Builder JA.
Those will compile your program into an executable/.NET Assembly/JAR file respectively, and all of them require the freely available MCR Runtime to be installed. In other words, the executables do not need to have a full MATLAB installation on the target machine, only the MCR runtime.
You would run the executable in the same manner as before.
Another product is the MATLAB Coder, which converts your MATLAB code into C++ program. When compiled, it can run without any external requirement.
A new product by MathWorks is MATLAB Production Server. Personally I know nothing about it :)
Yet another option is to use TCP/IP to communicate between PHP and MATLAB. A server would be run on the MATLAB side, using socket programming written as C MEX-file or a Java class. See:
MATLAB Mex Socket Wrapper Library
Writing Java's pw.println(), etc. in MATLAB
The client being your PHP application. The idea is to have MATLAB listening for connections, reading whatever input is given by a client, eval it, and return the result. This is more involved than the other options, as you have to deal with serialization and other things like concurrency. The advantage is that MATLAB can be run on a separate server, even on multiple servers on the cloud (see this post).
So first, decide what approach best suits your project, then it would be easier to answer specific questions... Just always consult the documentation first, MATLAB toolboxes are very well documented and usually include many examples. Here are a couple more resources specific to MATLAB Compiler products family:
Webinar: Application Deployment with MATLAB
PDF File: MATLAB Application Deployment - Web Example Guide
Note that they concentrate on ASP.NET and Java JSP/servlet applications. In your case, the PHP application would communicate with a middle tier running a web service built using one of the above two options (or simply design a CGI-like site running plain executables built using the MATLAB Compiler as explained earlier).
To help the OP with running system commands from a PHP webpage, my post here is relevant (copied below).
We do exactly this all the time. I call them voodoo pages. Here's some working code:
<?php
$command="uptime"; $output; $retval; $errors="";
exec ($command, &$output, &$retval);
echo $output[0] . "\n";
unset($output);
?>
And the output to the webpage served:
13:40:19 up 22 days, 23:14, 0 users, load average: 0.04, 0.02, 0.00
And the additional note I added in the comments below:
Relative vs absolute paths may be a pain... $command might need to be /usr/bin/uptime or another could be /usr/bin/ls /home/chris/ftp. Normally, scripts' working directory is where they live in the file system. MATLAB is a windows program, yes? My experience is you will need absolute paths for the program and any files passed as arguments, example: $command="c:\\matlab\\matlab.exe c:\\www\\somefile.wav" And then single quotes required for silly NTFS names, TAB command line completion works well for examples. Or use proper 8.3 name with the ~ in it.
My answer would be in two parts:
How do I run a MATLAB script from the terminal?
I will give some example about running a MATLAB script from the terminal:
matlab -nojvm -nodesktop -r "run <the-script>.m"
matlab -nojvm -nodesktop -r "<the-script>"
matlab -nojvm -nodesktop -r "run <the/path>/<the-script>.m"
matlab in Windows must be in your environment path. How-to.
If you need to compile your script to Java:
java -jar yourjarfile.jar
How do I execute the terminal command from PHP?
I think the previous answers are good, and there isn't any need to repeat them.
More notes:
Watch for your security. You might be XSS'ed easily.
Abstract your code and improve it to save parameters and output to the database. Run your code in
Parallels or queue manager. You might create a REST service.
Unit test.
Use Linux. It's much more powerful.
One quick hack would be to compile your MATLAB code into an executable file then use PHP's shell_exec().
The difficult part would be adapting your MATLAB code (sorry, I didn't read it) in such a way that:
It will receive its input in command-line-interface style (as char strings);
It will output its results as text to standard output (file id #1 in MATLAB).
Then all it takes is to parse the MATLAB output back into PHP...

Writing a CLI for a php-based CMS (Similar to Drush)

I've been working on a CMS for a few years and I actually implemented a jquery-based console in the admin area, where you could do some handy things like enable/disable modules and so on.
I recently fiddled around with drupal and decided to install cygwin along with drush.
I tried googling around but figured this might be an unusual question: How does one go about creating a CLI for a php-based CMS? And exactly how does exactly drush worK? I mean, I know that it runs from the command line as a batch script in windows. But how does it interact with PHP and so on?
I do know some basic C# but this shouldn't be very hard once i figure out how this fits together. (php, sql, etc).
Any help is appreciated, thanks in advance :)
You can run a php cli from the terminal only when you have php compiled with cli support. Additional you need to specify an interpreter and pass the path to the script as argument. But you can also use a shebang #!/path/to/php. A better practise would be to use the env variable and not hardcode the path to php: #!/usr/bin/env php. Read here about it: http://tech.vg.no/2012/02/21/dont-hardcode-php-executable-path-in-shebang/.
Basically you can write a simple CLI shell with a infinite loop plus a 'exec()' or 'shell_exec()' PHP functions. you should get the user commands and send it to shell_exec() function for execute in a system shell and return the output of that to the user.
i.e:
while(TRUE){
if($input != 'exit')
$output=shell_exec($input);
else
break;
echo $output;
}
you can add other options and customize this simple loop.
you can call external programs with 'exec()' function.

Is it possible to use Python with php

I have a VPS Linux webserver with PHP installed.
I want to code some search engine or data mining stuff in one application which I am thinking of building in python.
I want to know if it is possible to use python and php together like calling functions of python in php and vice versa.
As it is my VPS server, I can install anything on that.
Has anyone tried using python in php? Are there any performance issues in real time??
You can execute python scripts using the exec() function in your php script.
Also this seems to provide an answer or two to your question.
Calling Python in PHP
You could have a look at PiP
To that end, I've [site author] written a Python extension for PHP. In short, this extensions allows the Python interpretter to be embedded inside of PHP (think of PHP as the parent language with Python as its child). This allows native Python objects to be instantiated and manipulated from within PHP. There is also initial support for accessing PHP functions and data from within the embedded Python environment.
However, I cannot comment on its reliability. Might need to test it for yourself.
You're trying to do something like
def helloWorld():
print 'Hello, World'
<?php helloWorld(); ?>
I'd say that you most certainly can't.
Edit: Have a look at php's shell_exec though.
I think that should be
<?php
$try = exec("/var/htdocs/folder/test.py")
?>
Try and see if it works
Better you have to do, use api, different application like one application on python server having python application and one server having php application with api. Like you store or delete data by using api in android or ios.

How to call a PHP method in Rails Application?

Is there any way to include php file using require and calling a php method in rails application ? I dont want to use phusion passenger as my server .
As far as I know, there is no PHP interpreters for Ruby, so your best bet would be to make an HTTP request from your Rails app to your PHP app to get its output. This of course won't allow you to directly call any specific function or such.
It might also be possible to run your Ruby application using JRuby, and at the same time, use Quercus. Quercus is a Java based PHP interpreter, so you might be able to interact through the JRuby Java interface with the Quercus parsed PHP script.
Disclaimer: Never used Ruby, JRuby or Quercus.
You could just run it through a shell command. There are several ways to do it, but here's one:
`php /path/to/your/script.php`
This will run the PHP script in the argument. If you care about the result of the script:
result = `php /path/to/your/script.php`
Disclaimer: I've never actually tried this so I don't know what it returns, but theoretically it should return whatever the script returns, if anything.

interfacing erlang application with php

I have a website built with PHP.
I have an Erlang application running as a daemon on the same server.
I need to call functions on the Erlang application from PHP and get back the result.
I've found PHP/Erlang and over PHP modules but I can't install a PHP module on this server, I can only use PHP code.
The only way I know to solve it is run an Erlang web server locally that the PHP will be able to talk to.
Is there a better way to solve it?
If using a httpd server is the best way, what Erlang server should I use?
It should be as light as possible and doesn't need features like SSL and doesn't need to handle large load.
Thanks
I'd run a webserver such as mochiweb hosting the erlang code. The PHP code would use curl to send http queries encoded in JSON to mochiweb. Mochiweb has a JSON encoder/decoder and PHP has native JSON support.
Even if every thing is on the same server, just use HTTP. Handles all the low level stuff and if you need to scale, it will be easier, as scaling with HTTP is a solved problem. Mochiweb is light and has high performance.
Erlang is excellent at socket I/O: maybe you could use a pipe of some sort?
This would be more direct than through another WEB server layer for sure.
Use the functions erlang:open_port and erlang:port_command functions to get data in/out of Erlang through a system port.
$ cat erl.erl
#!/usr/bin/env escript
main(Args) ->
io:format("~p\n", [Args]),
io:format("~p\n", [(catch test(Args))]).
test([N1,N2|_]) ->
lists:seq(list_to_integer(N1),list_to_integer(N2)).
$ chmod +x erl.erl
$ cat php.php
?php
var_dump(exec("./erl.erl 1 5"));
?>
$ php php.php
string(11) "[1,2,3,4,5]"
Have a look at erl_call. http://www.erlang.org/doc/man/erl_call.html
It is a unix program which is used to call a function in erlang. It will start a dummy erl node, execute the command(s) and return the result. You could use PHP to call erl_call and then use the results it returns.
I don't think there is better solution.
I need Erlang webserver to run it on web.
here is some info PHP+Erlang related
http://yaws.hyber.org/cgi.yaws

Categories