Jump to content

POST or Call LAN URL in background with Button


gogogadgets
Go to solution Solved by gogogadgets,

Recommended Posts

Hello, PHP Friends!

 

I wonder if somebody knows how to post or I better say call some URL like http://192.168.0.1/out.cgi?out1=1 from valid IP domain (server sees lan url) in background with Button click using form?

For example to turn some device ON or OFF...

 

I have tried with only PHP and also cURL but no luck :(

<?
//Check for status of device from xml (1 is Off & 0 is On)
if ($xml->out1 == 1) {
echo "<tr><td>OUT1</td><td><img src='images/red.png' alt='RED' height='23' width='23'></td><td><form method='post' action='index.php'><input type='hidden' value='out1' name='out1'><input type='submit' value='TURNON' name='TURNON'></form></td></tr>";
} else {
echo "<tr><td>OUT1</td><td><img src='images/green.png' alt='GREEN' height='23' width='23'></td><td><form method='post' action='index.php'><input type='hidden' value='out1' name='out1'><input type='submit' value='TURNOFF' name='TURNOFF'></form></td></tr>"; }

//cURL function to call URL with parameters
function post_to_url($url, $data) {
   $fields = '';
   foreach($data as $key => $value) {
      $fields .= $key . '=' . $value . '&';
   }
   rtrim($fields, '&');
   $post = curl_init();
   curl_setopt($post, CURLOPT_URL, $url);
   curl_setopt($post, CURLOPT_POST, count($data));
   curl_setopt($post, CURLOPT_POSTFIELDS, $fields);
   curl_setopt($post, CURLOPT_RETURNTRANSFER, 1);
   $result = curl_exec($post);
   curl_close($post);
}
//device URL
$url = 'http://192.168.0.1/out.cgi';
//Turn on device
$on  = $_POST['name'] . '=0';
//Turn off device
$off = $_POST['name'] . '=1';

//Check for Button action on user Click
if(isset($_POST['TURNON']))
{
  post_to_url($url,$on);
}elseif(isset($_POST['TURNOFF'])){
  post_to_url($url,$off);
}else{}

?>

If I should use any other method, way, function, shell_exec etc..?

 

Any suggestions, help is very welcome.

 

Thank you.

 

 

 

 

 

 

Link to comment
Share on other sites

Since you are initiating from a button, this means you are initiating from the client.  Why not just use Ajax to start the routine?  Or, is it a different domain?  If so, you have same origin policy to deal with.  I believe there are workarounds (Access-Control-Allow-Origin, jQuery's jsonp), but I believe there are security risks.

 

If you want to hit the originating server first, you can use curl.  If you don't want to wait for the transaction to complete, you might either need to run a thread in the background, or send headers telling the browser the transaction is complete (the later is typically preferred).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.