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

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.

Related

Is it possible to run both PHP and JSP on my droplet

I would like to use PHP for some webpages and JSP for others. There will be no webpages which use both simultaneously, it's either 100% PHP or 100% JSP for each one. I'm using a LEMP server with Nginx. I've been advised to run Tomcat behind the Nginx server. But I still want Nginx to process all my PHP code. Also, is it possible for both my PHP and JSP webpages to access the same database?
Any help will be appreciated.
That would work fine. Your nginx config would configure different servers or locations, one to route to fpm or whatever your php backend is, another for JSP inside tomcat.
Databases are typically language agnostic and multi tenant by design; there's no reason you can't access the same database from any number of apps in any number of languages (Limited, of course, by physical resources). There may be exceptions, but I can't think of any, and MySQL definitely isn't one.
Whether it's a good idea to have multiple distinct code bases share a database schema is a very different question. But that's all architecture concerns. The database certainly doesn't care.

Web front-end for c++ code

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.

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.

Is it possible to use Apache Thrift on a regular web server?

I already have a web server that I pay for, and I want to expose some services on it using Thrift and PHP.
My question is: can I run a Thrift server using normal PHP that's hosted on the default port (the same way web pages are hosted) instead of having a separate PHP application that runs on some funky obscure port. This way I wouldn't have to change the server configuration (which is something I'm not able to do even if I wanted to).
Thanks
EDIT: maybe I should clarify a bit more. Once I've defined my service using a .thrift file, is it possible to:
Run the thrift code generator
Take the generated code and put it on my webserver
Create an index.php which says (in pseudocode) "create a new instance of the service, and handle incoming requests"?
Okay, well I have figured out the answer on my own!
If you use a TPhpStream on the server side, you are able to serve requests coming in as regular http requests.
Many thanks to Rob Wilkerson https://github.com/robwilkerson/Thrift-Client-Server-Example--PHP-.
I also blogged about how to implement a simple example with PHP and Python at http://willwarren.com/2012/01/24/creating-a-public-api-with-apache-thrift/

Cross language development problem

I'm working on a project that involves a database (My SQL), website (PHP) and a custom high performance server application (C++). The C++ application (and its accompanying client application) make up the main bulk of the project, with the database storing long term data for it. The website is primarily for displaying various statistics, and administration.
1) I want the PHP scripts and c++ application to be able to communicate in some way, since the database is only used for persistent data, and additionally the c++ application may cache some things so needs to be told to reload the data in some cases. It is highly likely they will be on different machines, and even possibly different OS's. I've been considering the idea that TCP may be the best option with some simple command - response protocol?
2) What is the best way to write the common database interface code once, and be able to use it from both the PHP website and the c++ applications?
1) Use the database to communicate. The C++ application can
select * from table where some_last_modified_timestamp > '<last time checked>';
2) Use stored procedures in preference to hardcoded queries both in PHP and C++.
You might try not allowing PHP to access the database at all. Make the C++ app do all the database work, and make it serve data to the PHP site. You could run part of the C++ app as a server for the PHP to fetch reports etc from it.
#1: If you're on different OSes, then TCP sounds like a decent idea.
#2: Sounds like you need a C library, and then call that from both C++ (trivial) and PHP. A search on Google returns lots of articles about writing PHP extensions in C, for example:
http://devzone.zend.com/article/1021
http://www.devarticles.com/c/a/Cplusplus/Developing-Custom-PHP-Extensions-Part-1/
1) I would also suggest TCP. Depending on the complexity of request-response I'd probably pick either some ad-hoc text protocol or use XML (especially suitable if responses or requests are structured and more complex). If you use XML you won't need to write your own parsers/generators. You could even try using XML-RPC but I have no practical experience with that yet.
Best way to use the same SQL in both PHP and C++ is prepared statements.
A good way to communicate is for one to host a server (custom/soap/rest) which the other connects to. PHP can easily both host and connect, and since that code is written in C it should be easy in C++ too.
Writing a PHP extension like Eric Seppanen suggest is way beyond the scope and need of your project Id say.
Use Thrift or Protobufs (possibly Avro) to declare a communication protocol and use that over a tcp socket. It'll solve your cross language problems without having to roll a custom protocol, and you end up with actual objects on both sides (statically typed for c++!). I've seen Thrift used like this very successfully.
My approach is to use SWIG. I use it with python, but it supports PHP also.
Then it's very easy to use it from your script.
Other solutions could be some RPC (wich would allow to have the server and the PHP application in different places).
I am a beginner here, so please excuse if my idea is terribly bad.
But why cant we just transfer XML over TCP. Have a C++ TCP server and PHP TCP Client. I think PHP has a pretty powerful socket APIs

Categories