Receive JSON POST data in ZK 6 - php

I am writing an application that will send JSON data via POST with PHP cURL and then the data will be read by ZK 6 application and processed, how can I achieve this? Thanks.
[Edit]
I wanted to create a web service.

Since ZK applications are running in a Servlet Container (e.g. Tomcat/Jetty) you can just create/configure an HttpServlet implementation in your web.xml and post data to it using any kind of HttpClient ... be it a browser or a command line tool such as cURL or from PHP code.
From a ZK perspective however this doesn't seem to make much sense yet with the given information. A ZK application is running in a browser and depends on an HttpSession.
What you didn't tell is, where your application is currently running ... in an IFrame or in a headless browser at server side? Where do you want to send the information? To the overall application or a specific Session or even specific browsertab?

Related

How to talk between HTTP request & cli class objects

I have a application running that's listing to HTTP request. Each request is passed to single page where a framework object $app is instantiated and this takes care of routing / controller / model etc.
Now i have a another class whose object is instantiated via. a CLI script lets call it $cliApp now problem is how do i make both the object talk to each other. $app is instantiate every-time there is a new request.
But $cliApp is instantiate only once when script is ran. This scripts runs in loop via $loop object by PHP React Event loop.
Cli App is running websockets. So basically i want http & sockets to communicate via. http api.
P.s. :
Right now i have one solution to use message queueing e.g. 0mq etc. but that seems overkill since i'm not looking to scale and keep it simple.
Another solution i'm currently trying and feels right is to share a SptStorageObject between threads created by $http request and thread created by $cli request. Maybe this is question of dependency injection and i'm having troubles to share this $store object.
If I understand correctly, you have (assumptions noted):
a normal PHP web app that communicates over HTTP (presumably on Apache or similar webserver)
a long-running PHP cli app that communicates over websockets.
Presumably both apps are receiving communication from web clients on an ongoing basis. Presumably they also have their own persistent data stores, such as a MySQL database or similar, perhaps even sharing the same one.
I'm going to assume that what you need goes beyond each application accessing the most up-to-date data from the persistent data store (or that the two processes use separate data stores), and you actually need on-demand communication between the two processes.
You're on the right path with message queues, but as you note it's needless complexity to add a third dedicated inter-process communication layer when you've already got two communication layers that work perfectly fine on their own.
What you need is for your cli app to speak HTTP when it needs to initiate communication with your web app, and for your web app to speak web sockets when it needs to initiate communication with your cli app.
What this looks like in practice is fairly simple.
In your cli app, just use cURL to initiate an HTTP connection to your web app. This is fairly simple, there are endless resources out on the web to help you along the way and if you get stuck then coming here with a new question specific to your problem will get you going. All this requires in your web app is the following:
appropriate endpoint(s) which the cli app can send requests to, if the basic client facing pages won't suffice
some method to authenticate the cli app if it needs to access data that should not be available to web clients
For your web app to initiate a websocket connection to the cli app, it's a bit more complicated because I'm not aware of any native PHP functionionality that specifically targets the websocket protocol. However, I did find this (extremely permissive) github project that purports to give you the ability to set up a web socket server, and it also includes a client script that you could use to connect to and send/receive data while your web app process lives, and then shut it down when you're done. It appears to still have some minimal activity, you could use that directly or use it as a starting point to write your own websocket client.
In this case, just as in the reverse situation, you need the cli client to recognize and authenticate traffic from your web client so it can serve appropriate data just to it.
If for some reason this scenario won't work for you, then you're back to message queues or shared data stores (someone suggested redis, which can act as a hybrid data store/message queue under some circumstances).

Simple data exchange between servers

I am completely new to web development, so please forgive me in case this question is superfluous.
Here is what I am trying to do:
I have multiple Arduinos (equipped w/ EthernetShields) that are collecting and displaying data (using sensors and LCD screens). All Arduinos communicate with a webserver hosting a MySQL database and a webpage visualizing the data. The Arduinos are themselves capable of hosting minimalistic webservers that can run some simple html/php/etc...
My question is:
What is the most straightforward way to implement the communication between the Arduinos and the main server, so that I can send data back and forth between them, without any user interaction?
The data consists of relatively few integer values and short strings of text. Security is not an issue.
Edit for clarity:
How to continuously send data back and forth between two web-servers?
Assuming that I use PHP, what is a simple way to do this? All tutorials for the GET and POST methods included the use of 'form action', which to my understanding requires user interaction.
If I understand you right, you have a central web server that needs to communicate with a whole bunch of arduinos. You need to send information in both directions between the arduinos and the central server. And, you want to use web (http / tcp / ip ) protocol to do this.
You face a choice:
Do you want to have the central server initiate the communication? Or do you want to have each arduino initiate the communication?
I think the second choice is pretty good. It means that you can add new arduinos to your system without somehow reconfiguring the central server. But, I don't know much about your application so there might be some reason this is a bad idea.
So, what you do is implement a simple web CLIENT (not a browser) on each arduino. Then, on a regular schedule you have the each arduino do a web request to the main server. Depending on the amount of data you need to send from the arduino to the server, you can use a GET or a POST. If the data is small, you can use a GET request. For example if it's temp and humidity you can send the request like this once a second.
http://server/upload.php?temp=65&humidity=78
In response to this, the central server can handle this data correctly. You'll be able to tell which arduino it's from by the client internet address REMOTE_ADDR.
Then, the server sends a response that contains whatever data the server needs to send to the arduino.
See how this goes? each second each arduino hits the web server saying "here's what I have for you. What have you got for me?"

How to update a webpage (only) when something changed on the server side

I have a simple php page that displays data from a mysql database. I want it to update itself automatically when data gets changed on the server. (I don't want to update the page periodically at fix time intervals.) I guess I need the technology behind FB chat box or omegle. But I don't know how to implement it on php and mysql. I would be grateful if you could help me. Thanks.
You would need to look into WebSockets or a Comet server (which uses long-polling techniques) to accomplish a push system. Alternatively, instead of using push-like notification, you could make frequent polls to the server with nothing more than a request identifier and a timestamp, let the server decide if there is anything new since the last poll, and serve up the data if there is.
you can implement Comet technology which is opposite of Ajax. JavaScript Dojo Toolkit can be useful to handle this method well.
Dojo WebSocket
http://dojotoolkit.org/features/1.6/dojo-websocket
http://cometd.org/
"Comet is a web application model in which a long-held HTTP request allows a web server to push data to a browser, without the browser explicitly requesting it."
http://en.wikipedia.org/wiki/Comet_(programming))

Using an API - cURL to localhost vs jquery

I am currently working on a site. I designed it as an API so that it can easily interface with mobile devices as well as keep me completely separate from front end development. The intention was that the front end designer would use javascript\jQuery to make API calls. The API returns JSON so the front end designer would format the content appropriately. I noticed that instead of using jQuery to obtain this data he is using inline PHP to make the appropriate API calls using cURL to localhost and then echoing the JSON result and formatting it. Is this cause for concern since the server is essentially requesting itself. A new process is spawned, the server has to process a request AND response, etc. Is it better off for the remote clients to use jQuery to resolve the API calls or have the server cURL localhost and resolve them?
It sounds like the performance issue here would be that PHP might be getting loaded up more than it needs to:
JQuery -> RESTful API built on PHP
vs
JQUERY -> PHP cURL Call -> cURL -> RESTful API built on PHP
Each call takes an extra use of PHP, as you said spawning another process. The extra use of cURL is no biggie (it's lightweight), but the extra use of PHP might be an issue if you are going to have heavy usage (say 100 concurrent, but really depends on your server, and many other factors).

PHP client communicate with C++ Server over TCP

Im trying to get a Php page to be able to communicate with a C++ server. It must be able to recive and send at all time (and the same time). When the php page recives a string it shall make some changes in Mysql db but at the same time send other strings to the server.
You can think of it as a chat, but then the php recive a string it shall be formated.
The C++ server is up and running only the php client side is a mystery for me.
Is there any example code that anyone can help me with?
Thx!
Well first of all you need to understand and remember that PHP in itself is stateless. You can have PHP receive a request and parse it and do something with the gained information, but after that the request closes.
One solution you could try is having your C++ app HTTP POST to a PHP script. This script can parse and analyze the request and take action upon it.
Store something in the database
Create a response
Output the response
The C++ app can then do something with the response.
This will work but if your actual use case is a chatroom or something of the likes there are better solutions than using PHP.

Categories