0

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?

Appel Flap
  • 261
  • 3
  • 23
  • Is the code at 192.168.2.10 running on the live server as well and did you update the IP address? – Nigel Ren Jan 04 '20 at 20:22
  • @NigelRen The application at 192.168.2.10:12800 is indeed running on a server locally, and is waiting for incoming data sent to the IP address. – Appel Flap Jan 04 '20 at 20:32
  • You are unlikely able to reach this IP address from a remote server, the range you are using is only available on your local network. Not sure if https://stackoverflow.com/questions/52137672/how-do-i-make-my-ip-public-externally-can-access-my-localhost gives the answer. – Nigel Ren Jan 04 '20 at 20:37

1 Answers1

0

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.

Bartosz Pachołek
  • 1,278
  • 1
  • 8
  • 17
  • Thank you for the explanation. This solution is meant for any website visitor, so it will likely not work out if everyone who visits my site has to go through these steps. Would handling this with JavaScript work, since that's client side and not server side? – Appel Flap Jan 04 '20 at 20:47
  • From what you are saying every client would need to have the PHP file hosted on his/her machine and webserver enabled and that is most likely not what you would like to achieve. You can try to call a local IP via JavaScript, but still: will all your clients have a local webserver which will handle the request enabled? Maybe describe a bit more the service which you are trying to build so I will tell you more about the infrastructure which you require. – Bartosz Pachołek Jan 04 '20 at 20:50
  • Yes, every visitor will have a webserver running to receive incoming data. This service will be intended as a CDN to download update files to the visitors' server, always running on a local IP. I'm not sure if this is enough information for you to give advice on? Assumingly, JavaScript could work but that would require for me to expose a private API, so the next step would be for me to look into API security once I got a PoC working of the JavaScript method. – Appel Flap Jan 04 '20 at 20:54
  • Well, then yes: you can try to load contents using JSONP or by adding `Access-Control-Allow-Origin` header in the responses of all the local servers. But then still: the client needs to a) have the webserver ON while b) entering the website which calls the query to their local servers. – Bartosz Pachołek Jan 04 '20 at 21:01
  • That will not be a problem as the visitors knows the service, and requires to have a webserver running in order to use the service. I will try the JavaScript way of doing it. I thought sending it using PHP would be a lot easier for me since I wouldn't have to mess with securing APIs, oh well. Learned something new today. – Appel Flap Jan 04 '20 at 21:12
  • I will mark your question as accepted since it works using your method. For my situation, it's however not ideal and better to use a different method. – Appel Flap Jan 04 '20 at 21:15