php - How can i get the country's ISD code from IP -
i need countries isd code of site visitor based on ip location there library or code can integrate it.
can please check if below code snippet can usefull getting requirements done.
function ip_info($ip = null, $purpose = "location", $deep_detect = true) { $output = null; if (filter_var($ip, filter_validate_ip) === false) { $ip = $_server["remote_addr"]; if ($deep_detect) { if (filter_var(@$_server['http_x_forwarded_for'], filter_validate_ip)) $ip = $_server['http_x_forwarded_for']; if (filter_var(@$_server['http_client_ip'], filter_validate_ip)) $ip = $_server['http_client_ip']; } } $purpose = str_replace(array("name", "\n", "\t", " ", "-", "_"), null, strtolower(trim($purpose))); $support = array("country", "countrycode", "state", "region", "city", "location", "address"); $continents = array( "af" => "africa", "an" => "antarctica", "as" => "asia", "eu" => "europe", "oc" => "australia (oceania)", "na" => "north america", "sa" => "south america" ); if (filter_var($ip, filter_validate_ip) && in_array($purpose, $support)) { $ipdat = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=" . $ip)); if (@strlen(trim($ipdat->geoplugin_countrycode)) == 2) { switch ($purpose) { case "location": $output = array( "city" => @$ipdat->geoplugin_city, "state" => @$ipdat->geoplugin_regionname, "country" => @$ipdat->geoplugin_countryname, "country_code" => @$ipdat->geoplugin_countrycode, "continent" => @$continents[strtoupper($ipdat->geoplugin_continentcode)], "continent_code" => @$ipdat->geoplugin_continentcode ); break; case "address": $address = array($ipdat->geoplugin_countryname); if (@strlen($ipdat->geoplugin_regionname) >= 1) $address[] = $ipdat->geoplugin_regionname; if (@strlen($ipdat->geoplugin_city) >= 1) $address[] = $ipdat->geoplugin_city; $output = implode(", ", array_reverse($address)); break; case "city": $output = @$ipdat->geoplugin_city; break; case "state": $output = @$ipdat->geoplugin_regionname; break; case "region": $output = @$ipdat->geoplugin_regionname; break; case "country": $output = @$ipdat->geoplugin_countryname; break; case "countrycode": $output = @$ipdat->geoplugin_countrycode; break; } } } return $output; } function getcountry($code) { $countryarray = array( 'ad'=>array('name'=>'andorra','code'=>'376'), 'ae'=>array('name'=>'united arab emirates','code'=>'971'), 'af'=>array('name'=>'afghanistan','code'=>'93'), 'ag'=>array('name'=>'antigua , barbuda','code'=>'1268'), 'ai'=>array('name'=>'anguilla','code'=>'1264'), 'al'=>array('name'=>'albania','code'=>'355'), 'am'=>array('name'=>'armenia','code'=>'374'), 'an'=>array('name'=>'netherlands antilles','code'=>'599'), 'ao'=>array('name'=>'angola','code'=>'244'), 'aq'=>array('name'=>'antarctica','code'=>'672'), 'ar'=>array('name'=>'argentina','code'=>'54'), 'as'=>array('name'=>'american samoa','code'=>'1684'), 'at'=>array('name'=>'austria','code'=>'43'), 'au'=>array('name'=>'australia','code'=>'61'), 'aw'=>array('name'=>'aruba','code'=>'297'), 'az'=>array('name'=>'azerbaijan','code'=>'994'), 'ba'=>array('name'=>'bosnia , herzegovina','code'=>'387'), 'bb'=>array('name'=>'barbados','code'=>'1246'), 'bd'=>array('name'=>'bangladesh','code'=>'880'), 'be'=>array('name'=>'belgium','code'=>'32'), 'bf'=>array('name'=>'burkina faso','code'=>'226'), 'bg'=>array('name'=>'bulgaria','code'=>'359'), 'bh'=>array('name'=>'bahrain','code'=>'973'), 'bi'=>array('name'=>'burundi','code'=>'257'), 'bj'=>array('name'=>'benin','code'=>'229'), 'bl'=>array('name'=>'saint barthelemy','code'=>'590'), 'bm'=>array('name'=>'bermuda','code'=>'1441'), 'bn'=>array('name'=>'brunei darussalam','code'=>'673'), 'bo'=>array('name'=>'bolivia','code'=>'591'), 'br'=>array('name'=>'brazil','code'=>'55'), 'bs'=>array('name'=>'bahamas','code'=>'1242'), 'bt'=>array('name'=>'bhutan','code'=>'975'), 'bw'=>array('name'=>'botswana','code'=>'267'), 'by'=>array('name'=>'belarus','code'=>'375'), 'bz'=>array('name'=>'belize','code'=>'501'), 'ca'=>array('name'=>'canada','code'=>'1'), 'cc'=>array('name'=>'cocos (keeling) islands','code'=>'61'), 'cd'=>array('name'=>'congo, democratic republic of the','code'=>'243'), 'cf'=>array('name'=>'central african republic','code'=>'236'), 'cg'=>array('name'=>'congo','code'=>'242'), 'ch'=>array('name'=>'switzerland','code'=>'41'), 'ci'=>array('name'=>'cote d ivoire','code'=>'225'), 'ck'=>array('name'=>'cook islands','code'=>'682'), 'cl'=>array('name'=>'chile','code'=>'56'), 'cm'=>array('name'=>'cameroon','code'=>'237'), 'cn'=>array('name'=>'china','code'=>'86'), 'co'=>array('name'=>'colombia','code'=>'57'), 'cr'=>array('name'=>'costa rica','code'=>'506'), 'cu'=>array('name'=>'cuba','code'=>'53'), 'cv'=>array('name'=>'cape verde','code'=>'238'), 'cx'=>array('name'=>'christmas island','code'=>'61'), 'cy'=>array('name'=>'cyprus','code'=>'357'), 'cz'=>array('name'=>'czech republic','code'=>'420'), 'de'=>array('name'=>'germany','code'=>'49'), 'dj'=>array('name'=>'djibouti','code'=>'253'), 'dk'=>array('name'=>'denmark','code'=>'45'), 'dm'=>array('name'=>'dominica','code'=>'1767'), 'do'=>array('name'=>'dominican republic','code'=>'1809'), 'dz'=>array('name'=>'algeria','code'=>'213'), 'ec'=>array('name'=>'ecuador','code'=>'593'), 'ee'=>array('name'=>'estonia','code'=>'372'), 'eg'=>array('name'=>'egypt','code'=>'20'), 'er'=>array('name'=>'eritrea','code'=>'291'), 'es'=>array('name'=>'spain','code'=>'34'), 'et'=>array('name'=>'ethiopia','code'=>'251'), 'fi'=>array('name'=>'finland','code'=>'358'), 'fj'=>array('name'=>'fiji','code'=>'679'), 'fk'=>array('name'=>'falkland islands (malvinas)','code'=>'500'), 'fm'=>array('name'=>'micronesia, federated states of','code'=>'691'), 'fo'=>array('name'=>'faroe islands','code'=>'298'), 'fr'=>array('name'=>'france','code'=>'33'), 'ga'=>array('name'=>'gabon','code'=>'241'), 'gb'=>array('name'=>'united kingdom','code'=>'44'), 'gd'=>array('name'=>'grenada','code'=>'1473'), 'ge'=>array('name'=>'georgia','code'=>'995'), 'gh'=>array('name'=>'ghana','code'=>'233'), 'gi'=>array('name'=>'gibraltar','code'=>'350'), 'gl'=>array('name'=>'greenland','code'=>'299'), 'gm'=>array('name'=>'gambia','code'=>'220'), 'gn'=>array('name'=>'guinea','code'=>'224'), 'gq'=>array('name'=>'equatorial guinea','code'=>'240'), 'gr'=>array('name'=>'greece','code'=>'30'), 'gt'=>array('name'=>'guatemala','code'=>'502'), 'gu'=>array('name'=>'guam','code'=>'1671'), 'gw'=>array('name'=>'guinea-bissau','code'=>'245'), 'gy'=>array('name'=>'guyana','code'=>'592'), 'hk'=>array('name'=>'hong kong','code'=>'852'), 'hn'=>array('name'=>'honduras','code'=>'504'), 'hr'=>array('name'=>'croatia','code'=>'385'), 'ht'=>array('name'=>'haiti','code'=>'509'), 'hu'=>array('name'=>'hungary','code'=>'36'), 'id'=>array('name'=>'indonesia','code'=>'62'), 'ie'=>array('name'=>'ireland','code'=>'353'), 'il'=>array('name'=>'israel','code'=>'972'), 'im'=>array('name'=>'isle of man','code'=>'44'), 'in'=>array('name'=>'india','code'=>'91'), 'iq'=>array('name'=>'iraq','code'=>'964'), 'ir'=>array('name'=>'iran, islamic republic of','code'=>'98'), 'is'=>array('name'=>'iceland','code'=>'354'), 'it'=>array('name'=>'italy','code'=>'39'), 'jm'=>array('name'=>'jamaica','code'=>'1876'), 'jo'=>array('name'=>'jordan','code'=>'962'), 'jp'=>array('name'=>'japan','code'=>'81'), 'ke'=>array('name'=>'kenya','code'=>'254'), 'kg'=>array('name'=>'kyrgyzstan','code'=>'996'), 'kh'=>array('name'=>'cambodia','code'=>'855'), 'ki'=>array('name'=>'kiribati','code'=>'686'), 'km'=>array('name'=>'comoros','code'=>'269'), 'kn'=>array('name'=>'saint kitts , nevis','code'=>'1869'), 'kp'=>array('name'=>'korea democratic peoples republic of','code'=>'850'), 'kr'=>array('name'=>'korea republic of','code'=>'82'), 'kw'=>array('name'=>'kuwait','code'=>'965'), 'ky'=>array('name'=>'cayman islands','code'=>'1345'), 'kz'=>array('name'=>'kazakstan','code'=>'7'), 'la'=>array('name'=>'lao peoples democratic republic','code'=>'856'), 'lb'=>array('name'=>'lebanon','code'=>'961'), 'lc'=>array('name'=>'saint lucia','code'=>'1758'), 'li'=>array('name'=>'liechtenstein','code'=>'423'), 'lk'=>array('name'=>'sri lanka','code'=>'94'), 'lr'=>array('name'=>'liberia','code'=>'231'), 'ls'=>array('name'=>'lesotho','code'=>'266'), 'lt'=>array('name'=>'lithuania','code'=>'370'), 'lu'=>array('name'=>'luxembourg','code'=>'352'), 'lv'=>array('name'=>'latvia','code'=>'371'), 'ly'=>array('name'=>'libyan arab jamahiriya','code'=>'218'), 'ma'=>array('name'=>'morocco','code'=>'212'), 'mc'=>array('name'=>'monaco','code'=>'377'), 'md'=>array('name'=>'moldova, republic of','code'=>'373'), 'me'=>array('name'=>'montenegro','code'=>'382'), 'mf'=>array('name'=>'saint martin','code'=>'1599'), 'mg'=>array('name'=>'madagascar','code'=>'261'), 'mh'=>array('name'=>'marshall islands','code'=>'692'), 'mk'=>array('name'=>'macedonia, former yugoslav republic of','code'=>'389'), 'ml'=>array('name'=>'mali','code'=>'223'), 'mm'=>array('name'=>'myanmar','code'=>'95'), 'mn'=>array('name'=>'mongolia','code'=>'976'), 'mo'=>array('name'=>'macau','code'=>'853'), 'mp'=>array('name'=>'northern mariana islands','code'=>'1670'), 'mr'=>array('name'=>'mauritania','code'=>'222'), 'ms'=>array('name'=>'montserrat','code'=>'1664'), 'mt'=>array('name'=>'malta','code'=>'356'), 'mu'=>array('name'=>'mauritius','code'=>'230'), 'mv'=>array('name'=>'maldives','code'=>'960'), 'mw'=>array('name'=>'malawi','code'=>'265'), 'mx'=>array('name'=>'mexico','code'=>'52'), 'my'=>array('name'=>'malaysia','code'=>'60'), 'mz'=>array('name'=>'mozambique','code'=>'258'), 'na'=>array('name'=>'namibia','code'=>'264'), 'nc'=>array('name'=>'new caledonia','code'=>'687'), 'ne'=>array('name'=>'niger','code'=>'227'), 'ng'=>array('name'=>'nigeria','code'=>'234'), 'ni'=>array('name'=>'nicaragua','code'=>'505'), 'nl'=>array('name'=>'netherlands','code'=>'31'), 'no'=>array('name'=>'norway','code'=>'47'), 'np'=>array('name'=>'nepal','code'=>'977'), 'nr'=>array('name'=>'nauru','code'=>'674'), 'nu'=>array('name'=>'niue','code'=>'683'), 'nz'=>array('name'=>'new zealand','code'=>'64'), 'om'=>array('name'=>'oman','code'=>'968'), 'pa'=>array('name'=>'panama','code'=>'507'), 'pe'=>array('name'=>'peru','code'=>'51'), 'pf'=>array('name'=>'french polynesia','code'=>'689'), 'pg'=>array('name'=>'papua new guinea','code'=>'675'), 'ph'=>array('name'=>'philippines','code'=>'63'), 'pk'=>array('name'=>'pakistan','code'=>'92'), 'pl'=>array('name'=>'poland','code'=>'48'), 'pm'=>array('name'=>'saint pierre , miquelon','code'=>'508'), 'pn'=>array('name'=>'pitcairn','code'=>'870'), 'pr'=>array('name'=>'puerto rico','code'=>'1'), 'pt'=>array('name'=>'portugal','code'=>'351'), 'pw'=>array('name'=>'palau','code'=>'680'), 'py'=>array('name'=>'paraguay','code'=>'595'), 'qa'=>array('name'=>'qatar','code'=>'974'), 'ro'=>array('name'=>'romania','code'=>'40'), 'rs'=>array('name'=>'serbia','code'=>'381'), 'ru'=>array('name'=>'russian federation','code'=>'7'), 'rw'=>array('name'=>'rwanda','code'=>'250'), 'sa'=>array('name'=>'saudi arabia','code'=>'966'), 'sb'=>array('name'=>'solomon islands','code'=>'677'), 'sc'=>array('name'=>'seychelles','code'=>'248'), 'sd'=>array('name'=>'sudan','code'=>'249'), 'se'=>array('name'=>'sweden','code'=>'46'), 'sg'=>array('name'=>'singapore','code'=>'65'), 'sh'=>array('name'=>'saint helena','code'=>'290'), 'si'=>array('name'=>'slovenia','code'=>'386'), 'sk'=>array('name'=>'slovakia','code'=>'421'), 'sl'=>array('name'=>'sierra leone','code'=>'232'), 'sm'=>array('name'=>'san marino','code'=>'378'), 'sn'=>array('name'=>'senegal','code'=>'221'), 'so'=>array('name'=>'somalia','code'=>'252'), 'sr'=>array('name'=>'suriname','code'=>'597'), 'st'=>array('name'=>'sao tome , principe','code'=>'239'), 'sv'=>array('name'=>'el salvador','code'=>'503'), 'sy'=>array('name'=>'syrian arab republic','code'=>'963'), 'sz'=>array('name'=>'swaziland','code'=>'268'), 'tc'=>array('name'=>'turks , caicos islands','code'=>'1649'), 'td'=>array('name'=>'chad','code'=>'235'), 'tg'=>array('name'=>'togo','code'=>'228'), 'th'=>array('name'=>'thailand','code'=>'66'), 'tj'=>array('name'=>'tajikistan','code'=>'992'), 'tk'=>array('name'=>'tokelau','code'=>'690'), 'tl'=>array('name'=>'timor-leste','code'=>'670'), 'tm'=>array('name'=>'turkmenistan','code'=>'993'), 'tn'=>array('name'=>'tunisia','code'=>'216'), 'to'=>array('name'=>'tonga','code'=>'676'), 'tr'=>array('name'=>'turkey','code'=>'90'), 'tt'=>array('name'=>'trinidad , tobago','code'=>'1868'), 'tv'=>array('name'=>'tuvalu','code'=>'688'), 'tw'=>array('name'=>'taiwan, province of china','code'=>'886'), 'tz'=>array('name'=>'tanzania, united republic of','code'=>'255'), 'ua'=>array('name'=>'ukraine','code'=>'380'), 'ug'=>array('name'=>'uganda','code'=>'256'), 'us'=>array('name'=>'united states','code'=>'1'), 'uy'=>array('name'=>'uruguay','code'=>'598'), 'uz'=>array('name'=>'uzbekistan','code'=>'998'), 'va'=>array('name'=>'holy see (vatican city state)','code'=>'39'), 'vc'=>array('name'=>'saint vincent , grenadines','code'=>'1784'), 've'=>array('name'=>'venezuela','code'=>'58'), 'vg'=>array('name'=>'virgin islands, british','code'=>'1284'), 'vi'=>array('name'=>'virgin islands, u.s.','code'=>'1340'), 'vn'=>array('name'=>'viet nam','code'=>'84'), 'vu'=>array('name'=>'vanuatu','code'=>'678'), 'wf'=>array('name'=>'wallis , futuna','code'=>'681'), 'ws'=>array('name'=>'samoa','code'=>'685'), 'xk'=>array('name'=>'kosovo','code'=>'381'), 'ye'=>array('name'=>'yemen','code'=>'967'), 'yt'=>array('name'=>'mayotte','code'=>'262'), 'za'=>array('name'=>'south africa','code'=>'27'), 'zm'=>array('name'=>'zambia','code'=>'260'), 'zw'=>array('name'=>'zimbabwe','code'=>'263') ); $data= $countryarray[$code]; return $data['code']; } $code=ip_info("visitor", "country code"); $isd= getcountry($code);
you value of isd in $code
Comments
Post a Comment