Post values to PHP, read the values coming back

Looking at all these examples with Twillio and such. I’m having a hard time understanding the concepts. I wish there were some simple examples of duplicating the HTTP_REQUEST node for GET and POST. I want to use the agent to just POST two values to a PHP script, the PHP script then posts two values back to the imp.

I can do it fine using HTTP_IN and HTTP_REQUEST using the planner (blueprint). I just need a hint at doing this using the agent(s).

Hi POST example I’m sending this to my php app. I use for testing

`// Agent POST
server.log(“Agent: v1.3”);
siteID <-200;
locID <-5;

server.log(“agent: impMsg:”+v+"|"+im.dateTime+"|"+im.rssi+"|"+im.vdd);
local am = “s|”+siteID+"."+locID+"|"+v+"|"+im.dateTime+"|Movement|Msg|"+im.rssi+"|"+im.vdd;
local body = http.urlencode({value=am});
local req =“”,{}, body);
// User to test
local res = req.sendsync();
server.log("agent res: "+res.statuscode);


// Get example returning JSON

`function sendVars(request,res){
local vars = {
“t”: “”+_tmp+"",
“tEng”: “C”,
“rh”: “”+_rh+"",
“rhEng”: “%”,
“rssi”: “”+_rssi+"",
“vdd”: “”+_vdd+""
“bssid”: “”+_bssid+"",
“mac”: “”+_mac+""

local jvars = http.jsonencode(vars);
res.header(“Access-Control-Allow-Origin”, “*”);
res.header(“ss”, “Example of my own HEADER ver”);


Thank you … I’ll experiment with the // Agent POST script.

The next one … what is that describing? Once I POST values to a PHP script, my PHP script will be POSTing back some values to the imp. The // Get example you’re showing is still about sending variables to PHP? Except doing it in a different way?

I’m just making sure that your examples are about going from the imp to a PHP script and not the other way around. I’ll also need to have the imp accept variables coming in from a remote PHP script.

Slowly I’ll grasp this concept. I think it’s the imp cloud (server) part that is foreign to me.

Ok what you need to check is the body in the response to your POST
local req ="",{}, body); local res = req.sendsync(); server.log("agent res: "+res.body); // Send to imp device.send("impUpdate",res.body);

Thanks … I’ll play with this over the next couple days.

My project is a PHP script I wrote to receive a WOEID code (location code for Yahoo weather API) from the imp, and POST back the sunrise|sunset time to the imp. My imp will control some special lighting using these times.

I couldn’t find a free API to use that worked properly, so I made my own.

I’ll post it all if I can get it working.
So far, it works with the “planner nodes”, not yet with the agents.

The main problem I’ve had with many of the sunrise|sunset API’s is the fact that you need to enter latitude and longitude, and then there were problems with daylight savings time, etc. My method only requires the WOEID code for your location. The Yahoo Weather XML for a WOEID has the correct times calculated.

I got the POST to work good. The imp sends (POSTs) values to the PHP script.

Now the PHP script sends values back to the imp.
I cannot figure out how to have the imp see the values.
The example they show on the devwiki isn’t very easy to understand.

Your example about … device.send(“impUpdate”,res.body);
There must be other code not shown? Does this refer to a whole new function within agent?

There seems to be two things I’m not getting. 1) Does it POST to the PHP script and wait for something to come back in the same function? Or, does it POST to the PHP script and go on with other things … when a PHP script POSTs values to the imp, how does the imp get the values?

In my PHP script, I post to the imp using the api url … example:

But that is no longer used is it? Isn’t that for the HTTP-IN node in the planner?

I’m so close. Once I get the imp to talk both ways to my PHP script, I’ll have it figured out. I’m so glad someone is taking time to walk me through it. Thanks controlCloud! I am very experienced at programming (PHP, Perl, C++) … I’m feeling so dumb that I can’t understand the Squirrel coding w/ the imp. I so wish that there were better examples in the devwiki.

Mlseim glad to help i cant code, if you saw my PHP code you fall about laughing. Iv’e spent a lot time working in control and event driven server side JS may be some things just click. So I’m clear
You POST from imp+agent you not interested in HTTP response
You POST from PHP to agent+imp

Take a look at this diagram that @apineda did

You don’t need to fire it back via a url - if you post to your php script you can then get the values back likes this (I’m sending back json in this example)

Agent code:

local response = http.get(""+woeid, {}).sendasync(function(response) { if (response.statuscode = 200) { // example fires back {"woeid":"12345678"} from server server.log("Raw JSON from server: "+response.body); // turn this into a Squirrel table local decodedJSON = http.jsondecode(response.body); // send the table to your device device.send("response",decodedJSON); } else { device.send("response","Problem getting feed"); } });

On the device you then put:

agent.on("response",function(value) { server.log(value.woeid); });

Hope that helps!

I got it! Finally!
Thanks to both of you!

I’m gathering all of my stuff, cleaning-up code, and I’ll post the whole thing.
And boy did I learn a lot these last few days!

Here is the project. I posted it on a website because this forum editor isn’t good at posting code.

See this:

The PHP script is active, so feel free to test it out with your own WOEID/timezone.