1

I am at wit's end here. I have set up a LAMP server for a project I've been working on recently (it's for school). I am trying to obtain the public IP address from my server with PHP using curl. I used following code to obtain it:

$ch = curl_init();

curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, "http://ipecho.net/plain");

$host = curl_exec($ch);
curl_close($ch);

It works great on localhost on my home PC. Sadly, the Ubuntu server doesn't comply. When I echo the result, I get a formatted message saying:

NOT FOUND: The request URL "/plain/" was not found on this server.

I don't really know what to do here. cURL should work too, I have installed the right package.

SIDENOTE:

I have also tried via file_get_contents() (Which also worked on my home PC) but on the server, it just returned following message:

Warning: file-get-contents("http://ipecho.net/plain") failed to open stream: HTTP request failed! HTTP/1.1 202 

Could these two things be related to the same problem?

Nissa
  • 4,636
  • 8
  • 29
  • 37
  • http://stackoverflow.com/questions/16748206/php-cannot-access-external-urls Try setting allow_url_fopen – tjfo Feb 22 '17 at 21:18
  • It has already been enabled by default. I forgot to mention that I already went to check if it was enabled before posting this question. ^^ – Naldorenses Feb 22 '17 at 21:30

1 Answers1

0

Sounds like the network may not be up. I would check if

wget -O- http://ipecho.net/plain

works from the ubuntu shell.

If that doesn't work check resolv.conf to make sure you have DNS working and check your network scripts to make sure you have the IP gateway set up right

Tom Esch
  • 36
  • 1
  • Hello, thank you for your answer! Sadly, it was something completely different that I stumbled upon on accident. It turns out, that the external service I was using isn't compatible with ubuntu by default (which would explain why it worked on windows and not on ubuntu). At least that's what it said when I tried using cURL in the terminal with the address. I switched to another service "checkip.amazonaws.com". Still thanks! :D – Naldorenses Feb 24 '17 at 16:23