0

I followed this site How do I retrieve the visitor's ISP through PHP? to get the ISP data, but it's not working anymore( failed to open stream: HTTP request failed). I also tried to use curt

$curl_handle=curl_init();
curl_setopt($curl_handle, CURLOPT_URL,'http://www.whatismyipaddress.com/ip/132.123.23.23');
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.15) Gecko/20080623 Firefox/2.0.0.15") );
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_USERAGENT, 'Your application name');
$query = curl_exec($curl_handle);
curl_close($curl_handle);

echo $query;

but I'm getting the following error:

Moved Permanently    
The document has moved here.

Does anybody know similar solution?


EDITED

The curl solution is working only on my localhost, unfortunately the web-host service doesn't allow that. Does anybody know a solution using file_get_contents?

Community
  • 1
  • 1
Mokus
  • 10,174
  • 18
  • 80
  • 122
  • 1
    Is it not just a 301 redirect to the non-www version? Also note the comment that this violates the [TOS](http://whatismyipaddress.com/terms-of-use). – cmbuckley Feb 20 '12 at 18:37

2 Answers2

2

Set the option

curl_setopt( $curl_handle, CURLOPT_FOLLOWLOCATION, true );

to follow header redirects.

JJJ
  • 32,902
  • 20
  • 89
  • 102
  • The curl solution is working only on my localhost, unfortunately the web-host service doesn't allow that. Does anybody know a solution using file_get_contents? – Mokus Feb 20 '12 at 18:50
  • Depends on the settings enabled on the remote server. check a phpinfo() output and look for allow_url_fopen. If it's on, then you can use file_get_contents on a URL. – cmbuckley Feb 22 '12 at 09:33
0

$_SERVER['REMOTE_ADDR']

Note that this may not work as you expect if the user is behind a NAT firewall.

John
  • 727
  • 5
  • 15
  • The OP wants the ISP, not the IP. – JJJ Feb 20 '12 at 18:42
  • this is why I'm using this: if ($this->validip($_SERVER["HTTP_CLIENT_IP"])) { return $_SERVER["HTTP_CLIENT_IP"]; } foreach (explode(",",$_SERVER["HTTP_X_FORWARDED_FOR"]) as $ip) { if ($this->validip(trim($ip))) { return $ip; } } if ($this->validip($_SERVER["HTTP_X_FORWARDED"])) { return $_SERVER["HTTP_X_FORWARDED"]; } elseif ($this->validip($_SERVER["HTTP_FORWARDED_FOR"])) { return $_SERVER["HTTP_FORWARDED_FOR"]; } elseif ($this->validip($_SERVER["HTTP_FORWARDED"])) { return $_SERVER["HTTP_FORWARDED"]; ... – Mokus Feb 20 '12 at 18:44