I've been trying to use cURL to send data to a local IP. This IP is waiting to receive incoming data.
For this, I used the following code:
if(isset($_GET['post'])) {
$data = array(
'type' => 'direct',
'packages' => []
);
$curl = curl_init('http://192.168.2.10:12800/api/install');
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-type: application/json'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
if (curl_errno($curl)) {
$error_msg = curl_error($curl);
}
echo curl_exec($curl);
if (isset($error_msg)) {
echo $error_msg;
} else {
echo 'OK!';
}
}
On localhost it works, but on a live website it doesn't.
I was thinking maybe it's not possible for non-localhost to communicate with localhost environments, but I'm unsure.
Is it possible to make my script send data to a local IP?
EDIT: Afterthought, do I have to switch to JavaScript to get this working?
In order to a remote server to communicate with your local infrastructure you would need to expose it via an external IP. So you need to:
1) set a routing of a port on your router (to passthru for example port 12800 to a particular IP in your local network - so for example if your external IP is 83.123.123.123 then router will passthru any traffic sent to 83.123.123.123:12800 to 192.168.2.10:12800. Usually such setting is called "routing" or "port forwarding" or "virtual servers" in router's settings.
2) learn your external ip. You can do it by going to for example: https://www.myexternalip.com/
3) if your IP is dynamic it may change from time to time. To avoid a problem you can set up a Dynamic DNS entry in one of the popular providers - like no-ip.com for example.