gogogadgets Posted February 16, 2015 Share Posted February 16, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/294640-post-or-call-lan-url-in-background-with-button/ Share on other sites More sharing options...
NotionCommotion Posted February 16, 2015 Share Posted February 16, 2015 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). Quote Link to comment https://forums.phpfreaks.com/topic/294640-post-or-call-lan-url-in-background-with-button/#findComment-1505796 Share on other sites More sharing options...
Solution gogogadgets Posted February 16, 2015 Author Solution Share Posted February 16, 2015 Thank you for suggestions...I managed to solve problem using PHP exec & shell wget command: function exec_wget($url) { exec('/usr/bin/wget -O - -q -t 1 "http://'. $url . '/' . addslashes($url) . '" >/dev/null 2>&1' ); ) Quote Link to comment https://forums.phpfreaks.com/topic/294640-post-or-call-lan-url-in-background-with-button/#findComment-1505799 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.