I'm in the process of analyzing a large chunk of data in matlab. The data is userdata which is freely available and which I want to use to make recommendations for users using my Internet service.
My question is: once I've written my algorithm in matlab what is the easiest/best way to integrate it into my php project /website?
Thanks in advance
Well, it really depends on what you are using Matlab for. In many ways, doing the same matrix transforms in PHP or Python would be a pain. Hence you were using Matlab. You can however keep everything in Matlab form by using the Matlab Web Server.
Simply compile down to a binary that can be run on your server. See the examples supplied by Matworks.
Related
I've been looking into this for several hours, but everything I've looked at seems rather daunting. I've been using PHP for all of the simple stuff on my website so far. I'm doing a lot of statistical work, and I would like to have c++ available for the more intense calculations.
The c++ would be running locally on the same Unix machine as PHP.
Something like the following is what I'm at a loss for how to do:
<?php
//c++ program has a counter initialized to 0
//PHP tells c++ to add 5 to the counter. $incremented is 5
$incremented = increment_in_cpp_and_return(5);
//$incremented_again will be 7
$incremented_again = increment_in_cpp_and_return(2);
?>
Of course, I'm running some monte-carlo simulations and traversing really big trees instead of incrementing numbers, but that's not what's holding me back.
C++ just needs to listen for a number and return another number (maybe some stuff in JSON at most). It is important for the c++ to keep track of its variables between calls.
I've done a lot of reading on TCP, socket programming, etc and I'm just a little doubtful that this as complicated as the examples make it out to be. A lot of things have pointed me to this https://beej.us/guide/bgnet/html/multi/clientserver.html#simpleserver
If it really is more than 100 lines of c++, are there some popular libraries, or is there a simple implementation in another language?
Thanks!
If you only want to access your C++ program from PHP (or use PHP as the web frontend for your C++ code), an alternative to communicating over a socket would be to embed the C++ code into PHP as an extension.
There's a fair amount of boilerplate code associated with it, but most of it is generated for you by the ext_skel script (included in the PHP source).
Most information online about writing PHP extensions relates to using C, see Extending PHP with C++? for a couple of gotchas related to using C++ for this.
If your C++ is executeable, you could open it as a program, pass data to it via STDIN and pass the return value back to PHP via STDOUT. See proc_openDocs.
Your standard C++ library should offer access to STDIN and STDOUT, so you already have what you need.
I'm a little confused about how the c++ keeps its state between calls. Can proc_open be used to pass via STDIN to a c++ program that is continuously running? I'm worried that it starts a new instance of the c++
You might be looking for a Named pipeWikipedia, a form of inter-process communication (see as well: What are named pipes?), that is supported by most operating systems. It's simple I/O (FIFO) and similar compared to STDIN and STDOUT. You can keep your executable in memory while the other processes can send data to it.
Another simple way is to use sockets, those are supported by PHP as well and should be with your C/C++. Sockets will work across different machines, so you can run your (memory/CPU intensive?) own executable on a dedicated server that does only the calculation for example. Just looks what suits better in your case, from the comment I read you're looking for interprocess communication.
posix_mkfifoDocs (including sample PHP code in user-notes)
Sockets Tutorial - a simple tutorial on using sockets for interprocess communication (Linux Howtos)
Introduction to Named Pipes (Linux Journal)
Introduction to Interprocess Communication Using Named Pipes (Sun Developer Network)
(these are just some resources, you naturally can find more with a little research, I think for both named pipes and sockets you should be able to find source-code examples for your case)
You could try:
exec()
You send the data from PHP as arguments for the C++ written program that will be executed and the program will return the output so you can use it in PHP after the C++ program's execution.
I've been using gSOAP for remote procedure calls between C++ and PHP, and if you're using PHP5, the interaction is made very easy; see http://www.php.net/manual/en/class.soapclient.php
I have a set of ~5 ActionScript 3 classes that are currently used within a flex 4 application. Although their output is used to display graphs etc in my flex app, the classes themselves have no visual components - they are only used to do complex math computations (I originally implemented them in AS3 in order to avoid constant server calls when computations were needed by the flex app).
However, I now want to make the same mathematical computation engine available on my linux server so the computations can be done within PHP. Is there any way at all to access the logic in these classes on the server? I would really like to avoid re-implementing the complex logic in PHP.
Thanks so much for any help you can give!
How many lines of code in your AS3 classes, and what kind of load do you need to handle?
If you're building anything for more than one-off use then the easiest route is probably porting your ActionScript to JavaScript. There aren't any automated converters that I know of but JavaScript and AS3 are so similar that unless your five classes have thousands of lines of code you should be able to make short work of it. Once you've ported it to JavaScript it'll be trivial to run in Node.js, directly through the VM of your choice, or even in the user's browser.
If you only need this to scratch and itch or for limited use you may be able to get away with running AS3 directly in Tamarin or redtamarin. However as far as I know neither of these are currently suitable for production use.
If you are using this in a high-availability, high traffic PHP app, however, I think you'll experience a lot less pain in the long run just porting your code to PHP. AS3 and PHP are similar enough in syntax that you could probably just do a straight port.
Finally, you can find some further discussion and links in this thread: Is it possible to create a 'command line' swf?
You can use redtamarin
http://code.google.com/p/redtamarin/
from a Linux server standpoint you will be able to run
your AS3 source code as CGI (either the AS3 script directly or compiled as ABC)
or you can also bundle your AS3 code into an exe that you will then call via PHP
or make your AS3 script as executable with binfmt_misc
http://code.google.com/p/redtamarin/wiki/RunningShellScripts#Registering_an_extension_as_non-native_binaries_(Linux_only)
here on production and development servers we use redtamarin
as scripts, to do our SVN hooks, automate tasks on linux servers etc.
as socket servers, http servers and CGI
as executable to reuse AS3 logic into our automated builds
etc.
look a bit in the documentation you will see you have a lot of options
to reuse your AS3 code: stdin/stdout/stderr, sockets, pipes, CGI, etc.
I was search for a video platform which I can use as a web-service and install locally on my local server to my PHP website.
I know we can run PHP shell commands but I need to implement a solid system with the Transcoder.
So, it should provide a API using REST or SOAP to convert videos, convert videos in efficient way. Create video thumbnails. If it a ruby or Python then it would be great and should be a free and open source one.
I could see those Software on Github, seems its good but having limitations. https://github.com/streamio/streamio-ffmpeg.
Is this a bad idea to use Perl or Python written application using with PHP? what are the available FOS software
Is this a bad idea to use Perl or Python written application using with PHP?
A bad idea? No. But it can be annoying sometimes.
If you really want to use a web service for this instead of just calling exec/proc_open like the rest of the world, you can go ahead and do that without too much concern.
Another option available to you is Gearman a distributed work queue/RPC service with bindings for many languages, including PHP, Python and Ruby. You can easily write a daemon in Ruby that uses that ffmpeg wrapper, and call methods directly from your PHP code. Thanks to how Gearman works, you can easily scale out the video encoding services by just adding more workers.
You can use FFMPEG binary for conversion/thumb extraction with command line like exec() in PHP
I have some Python scripts that I run on my desktop now for cutting up files. I want to put them on the web and write a simple front-end in PHP where a user uploads a file and it is passed as an argument to a python script on the web server and it is written out in chunks and the user can re-download the chunks.
I know a decent amount of PHP, but I do not see:
How to mix PHP and Python programmatically
Is it possible to have a webpage in python that can just call the python script? Can one have a GUI page that is like zzz.com/text.py as example
For http requests, you need to set your web server to hand over certain request to PHP and others to Python. From within PHP's scripts, if you need to call some Python executable scripts, use one of PHP's shell functions. e.g. exec()
Yes it is possible. The djangobook is a nice tutorial that covers this in one of the earlier chapters. It shows you how to run python as a cgi or with apache.
On a personal note, if you have time to dig deeper into Python, I'd strongly encourage you to do the whole thing in it, rather than mix things with PHP. My experience tells me that there are probably more cases where a PHP app needs some Python support rather than the reverse.
If the supporting language can do everything that the main language does, what's the point of using the main language?
Is it possible to have a C++ program running on a server that sits and waits to be passed arguments by a PHP process.
The C++ program would then process these arguments and return a result to PHP.
I’ve been searching the web and can see a couple of ways to run a C++ program from PHP but can’t find a way to interact with a C++ program that is already running.
The sort of C++ programs I’m thinking of are ones that might take time to set up e.g. They create a big data structure of some kind and so I don’t want to run them every time I need them. I want the C++ programs to create their data structures and then sit back and wait until asked by PHP for information from that data structure.
Thanks
Use named pipes.
https://web.archive.org/web/20140223054156/http://my.opera.com/zomg/blog/2007/08/29/php-and-named-pipes
and
C++ https://web.archive.org/web/20110926155246/http://ist.marshall.edu/ist480acp/namedpipes.html
Write a C++ listener that recognizes XML RPC or SOAP requests so it can process the requests natively and return the results to PHP as an XML RPC or SOAP response.
Here are some references:
http://xmlrpc-c.sourceforge.net/
http://www.cs.fsu.edu/~engelen/soap.html
Would something like SWIG be of use to you? (http://www.swig.org/Doc1.3/Php.html) You could wrap the functionality you want to use, to expose it to your PHP code.