This post describes using PHP with HTML on a web page to turn on/off three imp output pins.
There are two scripts you need to put into your website (as shown below). The only change you need to make is to enter YOUR imp URL … the codes from the HTTP IN box on your planner.
The point of this post is to describe how to use a web page to affect an imp output without anyone seeing your imp URL. The imp code itself is not described here. The imp code I used is a modified version of the “porch light button”.
As of now, for the HTTP IN, you use the URL like this: https://api.electricimp.com/v1/… ?value=1
Doing that on a webpage, using a form, anyone can see the URL for which you POST the form. Not very secure.
===================================================================
EDIT:
As I discovered, this can be done with one script as MikeyDK figured out.
http://forums.electricimp.com/discussion/comment/3365#Comment_3365
I’ll leave my post and scripts on here … but the “single script” method takes care of all my woes.
Thanks MikeyDK … I think this will help other people.
So, with PHP, you can use CURL to post the value to the imp http api. Nobody can see the URL. One problem with posting values to a URL is that fact that after the post is sent, the PHP script can’t redirect after the CURL (headers already sent error). You might notice the same thing happening with apps (like Android). The imp returns the “OK” and your website script is done. You can’t get back to your html form page without hitting your browser back button.
To get around this, you can reference the https://api.electricimp.com/v1/… ?value=1 URL as an image. The act of referencing that as an image performs the HTTP IN on the imp side, and is a part of the HTML on your webpage - the same webpage that has the form.
Example, putting this in your HTML webpage will post the value to your HTTP IN:
Two things will happen when you use the imp URL in the src tag:
- Again, people can see the imp URL when they view the HTML source. We don’t want people to see that URL.
- The image on the browser is a box with the [x] in it … it’s not a real image (appears as a broken image).
So, you create a webpage, we’ll call it “index.php”. You have a form which calls the same script as itself. When you select a radio button, it references another PHP script as the image. I don’t want the image to show (box with an [x] in it), so I use CSS to hide that on the screen.
Here is “index.php” …
I can turn on/off any of 3 output pins.
In my imp code, I look for the value and determine which pin to affect.
#image{
display:none;
}
<?php
if($_POST['v']==="90" || $_POST['v']==="91" || $_POST['v']==="80" || $_POST['v']==="81" || $_POST['v']==="70" || $_POST['v']==="71"){
$v=$_POST['v'];
$url = "send_imp.php?v=".$v;
echo "";
}
?>
OUTPUT 7:
ON
OFF
OUTPUT 8:
ON
OFF
OUTPUT 9:
ON
OFF
============================================================================
Here is the PHP script called “send_imp.php” …
This does the PHP CURL to post the value to the imp URL and returns back the fake image without the “header already sent” problem.
It is accomplishing this by using PHP GD to create a “fake” or “garbage” JPG image using the imp URL.
=============================================================================
<?php if(strlen($_GET['v'])>1){ // put your imp URL reference (the URL from your HTTP IN box) on this next line. $rURL = "https://api.electricimp.com/v1/b451e3adsf1/15s5sdf4a7/?value=".$_GET['v']; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "$rURL"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); $out = curl_exec($ch); curl_close($ch); $imgRes = imagecreatefromstring($out); header("Content-Type: image/jpg"); imagejpeg($imgRes, NULL, 70); imagedestroy($imgRes); exit(); } ?>=========================================