simple-location/ip2locationlite.class.php

67 lines
1.7 KiB
PHP
Raw Permalink Normal View History

2014-01-07 01:26:41 +00:00
<?php
2014-01-07 03:30:02 +00:00
# Patched to use CURL instead of file_get_contents
2014-01-07 01:26:41 +00:00
final class ip2location_lite{
protected $errors = array();
protected $service = 'api.ipinfodb.com';
protected $version = 'v3';
protected $apiKey = '';
public function __construct(){}
public function __destruct(){}
public function setKey($key){
if(!empty($key)) $this->apiKey = $key;
}
public function getError(){
return implode("\n", $this->errors);
}
public function getCountry($host){
return $this->getResult($host, 'ip-country');
}
public function getCity($host){
return $this->getResult($host, 'ip-city');
}
private function getResult($host, $name){
$ip = @gethostbyname($host);
// if(filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)){
if(filter_var($ip, FILTER_VALIDATE_IP)){
2014-01-07 03:30:02 +00:00
$url = 'http://' . $this->service . '/' . $this->version . '/' . $name . '/?key=' . $this->apiKey . '&ip=' . $ip . '&format=xml';
$ch =curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$xml = curl_exec($ch);
curl_close($ch);
//$xml = @file_get_contents('http://' . $this->service . '/' . $this->version . '/' . $name . '/?key=' . $this->apiKey . '&ip=' . $ip . '&format=xml');
2014-01-07 01:26:41 +00:00
if (get_magic_quotes_runtime()){
$xml = stripslashes($xml);
}
try{
$response = @new SimpleXMLElement($xml);
foreach($response as $field=>$value){
$result[(string)$field] = (string)$value;
}
return $result;
}
catch(Exception $e){
$this->errors[] = $e->getMessage();
return;
}
}
$this->errors[] = '"' . $host . '" is not a valid IP address or hostname.';
return;
}
}
?>