Web front-end for c++ code - php

I have c++ code that can be compiled under Linux, windows or Mac OS. The code compares two images. I would like to have its front end running on a browser and make available to the www.
I am familiar with hosting and dns and that is not the issue. what I can't seem to figure out is:
How do I invoke the script once the image is uploaded by users?
The results from the code needs to be displayed back to the browser. How can a callback be set up for this?
Is there a php solution? Or python (with flask)?

You can either call the C++ application from PHP with exec and then return to browser whatever result is there. This is quick and not good idea. Better approach is to have a service/daemon in C++ running and taking tasks from queue (like RabbitMQ for example). This is scalable solution but requires more effort to implement.

Why don't use CppCMS (http://cppcms.com/) ? CppCMS is a Free High Performance C++ Web Development Framework.
You can use it to handle HTTP request and file upload and easily integrate your code...

You can use sockets, and start listening on some port from C++ program, then from PHP you can connect and send/receive data to/from your program.

Related

PHP: "Callback server" for a Windows server?

I am programming PHP applications where I need to move processing routines from the client (browser) to the server. We have our own dedicated Windows servers.
Let us say that a shopper buys something and the system has to generate a nice and complex invoice PDF (and do a lot other things that takes some seconds) and after this send it to the client as fast as possible. Right now, I have running these time-consuming routines in a hidden Iframe and hoping that client is not breaking the routine by going to another page. It is not a good solution.
A much better solution would be to trigger some kind of software on the Windows server that does the processing instead of the browser (and does it instantly).
I could use "Scheduled Tasks" in Windows but the quickest it can run is each minute. I need something that can run instantly. Do you know what can do this on a Windows server? Like some kind callback server (software).
To make sure user navigation doesn't stop your scripting you can use ignore_user_abort(true);
You can also use exec create background process and run your php script
Example
exec("start /B php Notify.php");
To learn more about execfunction click here
You can find more information by googling stuffs.

AppMobi / Phonegap Alternative with PHP

Not really a coding question exactly, and not sure which stack site to throw this on, so here goes.
Im wondering if there is any framework similar to appmobi/phonegap in the sense everything is sandboxed and compiled into an app format for both iphone and droids. Where the sandboxed server comes with the ability to run PHP on it, I've been tryin to search all day and I've come up with nothing as of yet other then heaps of articles on appmobi/phonegap. This could be a free or paid for framework (preferably free/open source).
If theres no frameworks like that then is there a means of taking something like apache itself that acts and runs like a webserver where I can load php into it, and at the end of my project compile it into one final package for use in the mobile markets?
The problem is that iOS generally uses Objective-C and the Android uses Java, but both can use javascript, which is why phonegap works.
So, unless you can write two webservers, one for iOS and one for Android, and port PHP to run under your webserver, or, more likely, write an interpreter that will take PHP and transform it to run on your webserver, then it would work.
But, if you write a mobile web application and use PHP as the code on the server, then you can send javascript and have that run in a browser on both devices.
The best option would be to have most of your business logic be in REST services that are written in PHP. You can do this in PhoneGap by following this blog:
http://share.ez.no/blogs/thiago-campos-viana/rest-api-basic-http-auth-and-phonegap-using-jquery
Here is the main part of the article though, so you can see how easy it can be.
//10.0.2.2:80 is the localhost in android emulator, app needs internet access
$.get("http://10.0.2.2:80/ez_site/api/ezp/content/node/2",
function(data) {
//code goes here
});
By doing this then you can have most of your business logic in PHP, and have just the UI be in javascript, able to take advantage of the hardware on the phones.
No. Attempting to include a web server and PHP runtime inside of a mobile application would have serious performance implications.
The reason PhoneGap works is because Javascript can execute natively in mobile applications by using the built-in web browser's rendering engine.

socket.io + php + is this a realistic solution at this point in time

I have been reading about websockets and also about socket.io.
I understand websockets are not support but enough browsers yet to be realistically used.
socket.io has browser support but appears to need node.js. With my hosting solution I have some space on a shared server, very limited control over the server and have access to php but there is no node.js.
I have read that people can get socket.io to work with PHP.
Question: seeing I have very limited server access, not control over ports, command line etc is it realistic that I could get socket.io working with PHP or should I just stick with the AJAX calls (I'm currently using)?
Note: I've developed a chat solution and it appears it would be much more light weight if it was running with a socket solution.
Also - is there good security with Sockets.io?
thx
Trying to make PHP talk to socket.io I suspect will be a big task.
Certainly it's not a great idea to run websockets via a pre-fork or threaded apache. Any event based server should be OK if you can handle the number of PHP processes. A better approach would be to write an event based server in php. Like this one
i researched a few websocket server implementations. i read, that there is a way to use node.js in production. using apache many years, i also searched for a module to handle websockets within apache, but found only half-hearted stuff. nothing which seemed solid.
i stumbled upon yaws, which is a websocket capable server written in erlang, which has the advantages of beeing the right language for a highly parallel application such as the chat thing, and also because websockets should be well integrated within the main server.
now, i'm developing a php chat server, which uses a framework called ratchet. -> google for 'ratchet php'. they got a good tutorial.
i start it via the commandline, and although for you it should be possible to also start the ratchet server via a browser call, i would not recommend that.
so in your constrained hosting environment, i would stick to ajax.

How I create a web server that can interact with mobile applications?

Ok first of all I want to create a web server that can interact with php, Mysql or mango db and mobile applications.
What do you think is better to use Java or Python?
I would like some good articles about web servers and how to interact with php ... For example I managed to create an simple web server but I don't know how to run php on it , and how can I make a mobile app to connect on it...
I would really appreciate some tips about these sort of things and some good tutorials ...
You can use http://pear.php.net/package/HTTP_Server as webserver base. Executing PHP within that is as simple as running a CGI interpreter. You need the php-cgi binary and pass some environment variables and possibly the POST body. It's just important to invoke the php-cgi binary with the right environment parameters.
Also have a look at Nanoweb, an existing webserver in PHP. It comes with a mod_cgi implementation.

Communication between PHP and application

I'm playing with an embedded Linux device and looking for a way to get my application code to communicate with a web interface. I need to show some status information from the application on the devices web interface and also would like to have a way to inform the application of any user actions like uploaded files etc. PHP-seems to be a good way to make the interface, but the communication part is harder. I have found the following options, but not sure which would be the easiest and most convenient to use.
Sockets. Have to enable sockets for the PHP first to try this. Don't know if enabling will take much more space.
Database. Seems like an overkill solution.
Shared file. Seems like a lot of work.
Named pipes. Tried this with some success, but not sure if there will be problems with for example on simultaneous page loads. Maybe sockets are easier?
What would be the best way to go? Is there something I'm totally missing? How is this done in those numerous commercial Linux based network switches?
I recently did something very similar using sockets, and it worked really well. I had a Java application that communicates with the device, which listened on a server socket, and the PHP application was the client.
So in your case, the PHP client would initialize the connection, and then the server can reply with the status of the device.
There's plenty of tutorials on how to do client/server socket communication with most languages, so it shouldn't take too long to figure out.
What kind of device is it?
If you work with something like a shared file, how will the device be updated?
How will named pipes run into concurrency problems that sockets will avoid?
In terms of communication from the device to PHP, a file seems perfect. PHP can use something basic like file_get_contents(), the device can just write to the file. If you're worried about the moment in time the file is updated to a quick length check.
In terms of PHP informing the device of what to do, I'm also leaning towards files. Have the device watch a directory, and have the script create a file there with something like file_put_contents($path . uniqid(), $command); That way should two scripts run at the exact sime time, you simply have two files for the device to work with.
Embedded linux boxes for routing with web interface don't use PHP. They use CGI and have shell scripts deliver the web page.
For getting information from the application to the web interface, the Shared file option seems most reasonable to me. The application can just write information into the file which is read by PHP.
The other way round it looks not so good at first. PHP supports locking of files, but it most probably doesn't work on a system level. Perhaps one solution is that in fact every PHP script which has information for the application creates it own file (with a unique id filename, e.g. based on timestamp + random value). The application could watch a designated directory for these files to pop-up. After processing them, it could just delete them. For that, the application only needs write permission on the directory (so file ownership is not an issue).
If possible, use shell scripts.
I did something similar, i wrote a video surveillance application. The video part is handled by motion (a great FOSS package). The application is a turn-key solution on standardized hardware, used to monitor slot-machine casinos. It serves as a kiosk system locally and is accessible via internet. I wrote all UI code in PHP, the local display is a tightly locked down KDE desktop with a full screen browser defaulting to localhost. I used shell scripts to interact with motion and the OS.
On a second thought:
If you can use self-compiled applications on the device: Write a simple program that returns the value you want and use PHP's exec() or passthru() or system().

Categories