I want to replace http://www.wpfetish.com/ip2nation.php/?ip= with http://ipinfo.io/yourip_here/country
in this code to get country code
/*Uses WordPress HTTP Class*/
if( !class_exists( 'WP_Http' ) )
include_once( ABSPATH . WPINC. '/class-http.php' );
$request = new WP_Http;
/*
* get country info based on the ip of user
* requests to the plugin server for ip to country code
*/
$result = $request->request( "http://www.wpfetish.com/ip2nation.php/?ip=$user_ip" );
if(!empty($result['body'])){
$res = json_decode($result['body']);
if($res->status == 'success'){
return strtoupper( $res->iso_code_2 );
}
}
return 'default';
}
You'll need to change a few other things, because http://ipinfo.io doesn't return a body or status field:
$ curl ipinfo.io/8.8.8.8
{
"ip": "8.8.8.8",
"hostname": "google-public-dns-a.google.com",
"city": "Mountain View",
"region": "California",
"country": "US",
"loc": "37.3860,-122.0838",
"org": "AS15169 Google Inc.",
"postal": "94040"
}
And if you request just /country
it doesn't return a JSON object:
$ curl ipinfo.io/8.8.8.8/country
US
So the full code you need is just this:
$request = new WP_Http;
$result = $request->request("http://ipinfo.io/$user_ip/country");
return trim($result);
If you don't want to use WP_Http and want to check for some specific countries, here's the full code:
$user_ip = $_SERVER['REMOTE_ADDR'];
$res = file_get_contents("http://ipinfo.io/${user_ip}/country");
$country = trim($res);
if(in_array($country, array("GB", "US"))) {
// User is in the UK or US
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments