Get IP and geolocation class sharing in PHP, IP geolocation
Before we found a script that didn't finish, it simply expanded.
Getting IP is using classic logic, and geo-targeting uses the generic interface of Sina.
The use of the method see source code:
Copy CodeThe code is as follows:
<?php
/*
* GET location by SINA IP INTERFACE
*
* @SOULTEARY 2013.04.14
*/
Classip
{
Private $args =array ();
Function__construct ()
{
$this->args=self::init_args (Func_get_args ());
$ip =self::getip ();
$ret =preg_match_all ('/(\d+\.) {3}\d+/i ', $ip, $result);
if (! $ret) {
Returnfalse;
}else{
$result = $result [0];
}
if (isset ($this->args[' Onlyip ') && $this->args[' Onlyip ']==true) {
if (isset ($this->args[' format ') && $this->args[' format ']== ' JSON ') {
$result =json_encode ($result);
}else{
$result =implode (', ', $result);
}
if (isset ($this->args[' echo ') && $this->args[' echo ']==true) {
Echo$result;
Returntrue;
}else{
Return$result;
}
}else{
$apiURL = ' http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip= '. $result [0];
if (isset ($this->args[' format ') && $this->args[' format ']== ' JSON ') {
$apiURL. = ' &format=json ';
$return = $this->ipcurl ($apiURL);
}else{
$return = $this->ipcurl ($apiURL);
$return =iconv ("Gbk//ignore", "UTF-8", $return);
}
if (isset ($this->args[' echo ') && $this->args[' echo ']==true) {
Echo$return;
Returntrue;
}else{
Return$return;
}
}
}
Public Functioninit_args ($args)
{
$result =array ();
For ($i =0, $n =count ($args), $i < $n; $i + +) {
$result =self::associative_push ($args [$i], $result);
}
Return$result;
}
Public Functionassociative_push ($arr, $tmp)
{
if (Is_array ($tmp)) {
foreach ($tmpas $key=> $value) {
$arr [$key]= $value;
}
Return$arr;
}
Returnfalse;
}
Public Functiongetip ()
{
if (Isset ($_server[' http_x_forwarded_for ') &&$_server[' http_x_forwarded_for ']&&strcasecmp ($_ server[' http_x_forwarded_for '), ' unknown ') {
return$_server[' http_x_forwarded_for '];
}elseif (Isset ($_server[' remote_addr ') &&$_server[' remote_addr ']&&strcasecmp ($_SERVER[' REMOTE_ ADDR '], ' unknown ') {
return$_server[' REMOTE_ADDR '];
}
}
Private Functionipcurl ($url)
{
$ch =curl_init ();
curl_setopt ($ch, Curlopt_url, $url);
curl_setopt ($ch, curlopt_header,0);
Ob_start ();
Curl_exec ($ch);
Curl_close ($ch);
$result =ob_get_contents ();
Ob_end_clean ();
Return$result;
}
}
?>
<title>Demo</title>
CODE:
GetIP
' Onlyip ' =>true, ' ECHO ' =>true
<?phpnewip (Array (' Onlyip ' =>true, ' ECHO ' =>true));? >
' Onlyip ' =>true, ' FORMAT ' = ' JSON ', ' ECHO ' =>true
<?phpnewip (Array (' Onlyip ' =>true, ' FORMAT ' = ' JSON ', ' ECHO ' =>true));? >
GET Location
' ECHO ' =>true
<?phpnewip (Array (' ECHO ' =>true));? >
' FORMAT ' = ' JSON ', ' ECHO ' =>true
<?php Newip (Array (' FORMAT ' = ' + ' JSON ', ' ECHO ' =>true));? >
http://www.bkjia.com/PHPjc/955974.html www.bkjia.com true http://www.bkjia.com/PHPjc/955974.html techarticle implemented in PHP to obtain IP and geo-location sharing, IP geolocation found before a script is not finished, a simple extension of a bit. Get IP using classic logic, geo-positioning makes ...