$dev=array(
array('name'=>'Gdoor', 'esp_ip'=>'192.168.1.133', 'pin'=>3 ),
array('name'=>'ACunit', 'esp_ip'=>'192.168.1.133', 'pin'=>1 ),
array('name'=>'Light1', 'esp_ip'=>'192.168.1.134', 'pin'=>1 )
);
This creates a two dimensional array. An array of arrays if you will. Each element of the $dev array is an array consisting of a device name, the IP of the ESP that handles that device, and the pin number controlling the device. I use an array like this because it is easy to add/delete/modify devices in the list without knowing how to code PHP.
foreach ($dev as $k=>$v) {
This sets up a loop to go through each device definition in the $dev array one at a time.
$cmd=$_REQUEST[$dev[$k]['name']];
This line does a lot! It starts with the name of the device being examined by the loop ($dev[$k]['name]). It then attempts to pull from the command line sent to your server whatever value has been passed for that variable name. So it you passed, http://{ip_address}/{this_script}.php?Light1=1 then $cmd will be set to "1" IF the device entry we are looking at is Light1 - otherwise, $cmd will be blank. So this both looks for a device name and retrieves the value for it.
if ($cmd!='') {
If we haven't gotten to the "Light1" entry yet, then nothing happens and we loop past. If the device name was found, a 0, 1, or r is returned in $cmd and the remaining code executes:
$url="http://" . $dev[$k]['esp_ip'] . "/msg?";
Start constructing a URL to address the correct ESP for this device. The IP address comes from that device's array entry within the $dev array. So now we have http://{ESP_ip}/msg=? - We still need to add arguments to the URL.
if ($cmd!='r') $url .= "pin=" . $dev[$k]['pin'] . "&";
If the command (value returned for the device) is not an "r" (ie. 0 or 1 command to change device state) then we add pin={pin_number}& to the URL. {pin_number} is taken from the device definition in the $dev array. Notes: ".=" is an append operator in PHP. We add the "&" in anticipation of the next argument added below.
$url .= "stat=" . $cmd;
Now we add stat=x where x is 0, 1, or r, depending on what the command is for the device. We end up with a URL like, http://192.168.0.20/msg?pin=1&stat=0 or http://192.168.0.20/msg?pin=1&stat=1 or http://192.168.0.20/msg?stat=r
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($curl);
curl_close($curl);
CURL is a tool that fetches data from a URL, just like your browser. This code creates a CURL instance, calls the URL, and places the output in $output. This effectively sends a message to the right ESP with a pin number and state. When the ESP handles the pin, it sends any output desired back to CURL which relays it to $output.
print $output;
Finally, we send the output from the ESP back to the calling browser.
}
}
This just closes the "if" and "loop"
It'll work.
If you name this script "index.php" then you should be able to access your devices with a browser command like, http://{server_ip}/?Light1=1