Php cli need help

Hi, I need some help please I have batch file that call my php script…this php script is listening to connected socket…and i want to pass a parameter on it.but i am confuse how to do i send the parameter which is from the web…

example if i pressed sendcommand button in my web this parameter will send to the connected socket.



if(isset($_POST['comparam'])){
    $command = $_POST['comparam'];

     //$command - here i don't know what to do with the $command i don't know how to send this to my running batch file so that this will be send to the //client    connected in the socket.

}


I have this script already in my socket(this the running batch file)


if(isset($argv[1])){
   $mycommand = $argv[1];
   foreach ($clients as $send_sock) {
      socket_write($send_sock, $mycommand);
   }
}


I appreciate someone can help me.

Thank you in advance.

If you are invoking PHP from the command line there will be no $_POST, $_GET, $_SESSION, $_REQUEST or $_COOKIE superglobals - these are set by the webserver. $_SERVER[‘argc’] will contain the arguments passed to the script from the command line.

Yes I know that there are no $_POST, $_GET, $_SESSION, $_REQUEST or $_COOKIE superglobals ,but i am confuse how can i dynamically send comand from web to socket.

Okay I have this example
index.php





  <div>
    <form method='post' action='toMycommand.php'>
          <input type="submit" name="reboot" id="reboot" value="Reboot">
       </form>
  </div>


toMycommand.php


  
 if(isset($_POST['reboot']){
    
     $command = 'xxxxxxx';//this command is for rebooting the device..I want now to send this to socket how can i send this ?

      
}
     

I have running socket listener ->listener.php which executed by the batch file…I want now the $command to pass to my lisener.php…

I know i could not do this.

header(“Location:listener.php?comm=$command”);

Hi Jemz,

There are a couple of ways that I think you could send data from a web-based script to your CLI socket server:

  • Write the data to a message-queue type system that your socket server periodically checks for new messages.
  • Connect to the socket server as an additional client, and have your server accept the commands to pass to the other connected devices.

Hi fretbuner,

Thank you for the reply…I could not get what you mean for message-queue type system ?

In your No.2,How can i launch or call the cmd so that i can connect to server as a client ?Is this what you mean ?

Thank you in advance.

A MySQL DB could serve as simple message queue. Your admin script would add a row to the table with the command to be executed and a timestamp. Your socket script would check the table at regular intervals for new entries and, if it finds one, executes the command. Check out the wikipedia entry for more info on message queues.

Your socket server is continuously running, listening for connections from your GPS device, right? So you can connect to it via a socket, and send it the commands that you want relayed to the device.

@fretburner,

Thank you for this idea,I will let you know if this will solve my problem.I’ll be right back as soon as I can.

Hi @fretburner,…I got it now sending command from webpage to socket by connecting as a client…Thank you for this idea.