127.0.0.1 // [country_code] => // [country_name] => // [region_code] => // [region_name] => // [city] => // [zip_code] => // [time_zone] => // [latitude] => 0 // [longitude] => 0 // [metro_code] => 0 // ) // // $geoip_info = Array( // [ip] => 118.127.46.63 // [country_code] => AU // [country_name] => Australia // [region_code] => QLD // [region_name] => Queensland // [city] => Brisbane // [zip_code] => 4000 // [time_zone] => Australia/Brisbane // [latitude] => -27.485 // [longitude] => 153.02 // [metro_code] => 0 // ) // // On FAILURE // $error_message STRING // ------------------------------------------------------------------------- // ========================================================================= // We can deliver quick results, if we're running on localhost... // ========================================================================= if ( $target_ip === '127.0.0.1' ) { return array( 'ip' => '127.0.0.1' , 'country_code' => '' , 'country_name' => '' , 'region_code' => '' , 'region_name' => '' , 'city' => '' , 'zip_code' => '' , 'time_zone' => '' , 'latitude' => 0 , 'longitude' => 0 , 'metro_code' => 0 ) ; // This is the data returned by:- // https://freegeoip.net/ // // for IP = 127.0.0.1 } // ========================================================================= // Init. // ========================================================================= $ns = __NAMESPACE__ ; $fn = __FUNCTION__ ; $ln = (string) __LINE__ ; // ========================================================================= // Make sure we have a valid IP address... // ========================================================================= if ( ! is_string( $target_ip ) || trim( $target_ip ) === '' ) { return << array( // 'default' => 3, // value to return if the filter fails // // other options here // 'min_range' => 0 // ), // 'flags' => FILTER_FLAG_ALLOW_OCTAL, // ) ; // $var = filter_var('0755', FILTER_VALIDATE_INT, $options); // // // for filter that only accept flags, you can pass them directly // $var = filter_var('oops', FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); // // // for filter that only accept flags, you can also pass as an array // $var = filter_var('oops', FILTER_VALIDATE_BOOLEAN, // array('flags' => FILTER_NULL_ON_FAILURE)); // // // callback validate filter // function foo($value) // { // // Expected format: Surname, GivenNames // if (strpos($value, ", ") === false) return false; // list($surname, $givennames) = explode(", ", $value, 2); // $empty = (empty($surname) || empty($givennames)); // $notstrings = (!is_string($surname) || !is_string($givennames)); // if ($empty || $notstrings) { // return false; // } else { // return $value; // } // } // $var = filter_var('Doe, Jane Sue', FILTER_CALLBACK, array('options' => 'foo')); // // Returns the filtered data, or FALSE if the filter fails. // // (PHP 5 >= 5.2.0) // ------------------------------------------------------------------------- if ( filter_var( $target_ip , FILTER_VALIDATE_IP ) === FALSE ) { return << 127.0.0.1 // [country_code] => // [country_name] => // [region_code] => // [region_name] => // [city] => // [zip_code] => // [time_zone] => // [latitude] => 0 // [longitude] => 0 // [metro_code] => 0 // ) // // $geoip_info = Array( // [ip] => 118.127.46.63 // [country_code] => AU // [country_name] => Australia // [region_code] => QLD // [region_name] => Queensland // [city] => Brisbane // [zip_code] => 4000 // [time_zone] => Australia/Brisbane // [latitude] => -27.485 // [longitude] => 153.02 // [metro_code] => 0 // ) // // On FAILURE // $error_message STRING // ------------------------------------------------------------------------- $geoip_info = get_geoip_info_from_freegeoip_dot_net( $target_ip ) ; // ------------------------------------------------------------------------- if ( is_array( $geoip_info ) ) { return $geoip_info ; } return $geoip_info ; // As of early 2016, FreeGeoIP seems to be failing a lot. Ie, generates:- // "Connection timed out after 5000 milliseconds" // // errors. // // Since NO replacement (eg; telize.com") is yet available, we return // the original error message (because it's more descriptive of what // the real problem is). // ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: // FROM: "telize.com"... // ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: // ========================================================================= // FAILURE // ========================================================================= return << 127.0.0.1 // [country_code] => // [country_name] => // [region_code] => // [region_name] => // [city] => // [zip_code] => // [time_zone] => // [latitude] => 0 // [longitude] => 0 // [metro_code] => 0 // ) // // $geoip_info = Array( // [ip] => 118.127.46.63 // [country_code] => AU // [country_name] => Australia // [region_code] => QLD // [region_name] => Queensland // [city] => Brisbane // [zip_code] => 4000 // [time_zone] => Australia/Brisbane // [latitude] => -27.485 // [longitude] => 153.02 // [metro_code] => 0 // ) // // On FAILURE // $error_message STRING // ------------------------------------------------------------------------- // ========================================================================= // Send HTTP request to "freegeoip.net"... // ========================================================================= // ------------------------------------------------------------------------- // $response = wp_remote_get( $url, $args ) // - - - - - - - - - - - - - - - - - - - - // Retrieve the raw response from the HTTP request using the GET method. // Results include HTTP headers and content. // // See wp_remote_post() for using the HTTP POST method // // $args = array( // 'timeout' => 5, // 'redirection' => 5, // 'httpversion' => '1.0', // 'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' ), // 'blocking' => true, // 'headers' => array(), // 'cookies' => array(), // 'body' => null, // 'compress' => false, // 'decompress' => true, // 'sslverify' => true, // 'stream' => false, // 'filename' => null // ) // // $url // (string) (required) Site URL to retrieve. // Default: None // // $args // (array) (optional) Override the defaults. // Default: array() // // See HTTP API for more information on the arguments array format. // // RETURN VALUES // (WP_Error|array) // The response or WP_Error on failure. See wp_remote_post() for a // full example of response array format. // // CHANGE LOG // Since: 2.7.0 // ------------------------------------------------------------------------- $url = 'http://freegeoip.net/json/' . $target_ip ; // ------------------------------------------------------------------------- $response = wp_remote_get( $url ) ; // ------------------------------------------------------------------------- if ( is_wp_error( $response ) ) { return $response->get_error_message() ; } // ------------------------------------------------------------------------- // Here we should have (eg):- // // $response = Array( // // [headers] => Array( // [server] => nginx/1.4.6 (Ubuntu) // [date] => Mon, 23 Feb 2015 07:17:32 GMT // [content-type] => application/json // [content-length] => 170 // [connection] => close // [access-control-allow-method] => GET, HEAD, OPTIONS // [access-control-allow-origin] => * // [x-database-date] => Fri, 06 Feb 2015 16:56:08 GMT // ) // // [body] => {"ip":"127.0.0.1","country_code":"","country_name":"","region_code":"","region_name":"","city":"","zip_code":"","time_zone":"","latitude":0,"longitude":0,"metro_code":0} // // [response] => Array( // [code] => 200 // [message] => OK // ) // // [cookies] => Array() // // [filename] => // // ) // // ------------------------------------------------------------------------- //\greatKiwi_byFernTec_adSwapper_local_v0x1x211_testDebug\pr( $response , '$response' ) ; // ------------------------------------------------------------------------- if ( ! array_key_exists( 'response' , $response ) || ! array_key_exists( 'code' , $response['response'] ) || $response['response']['code'] != 200 || ! array_key_exists( 'body' , $response ) || ! is_string( $response['body'] ) || trim( $response['body'] ) === '' ) { return <<= 5.2.0, PECL json >= 1.2.0) // ------------------------------------------------------------------------- $assoc = TRUE ; // ------------------------------------------------------------------------- $geoip_info = \json_decode( $response['body'] , $assoc ) ; // ------------------------------------------------------------------------- $errno = \json_last_error() ; // ------------------------------------------------------------------------- if ( $errno !== JSON_ERROR_NONE ) { return << 127.0.0.1 // [country_code] => // [country_name] => // [region_code] => // [region_name] => // [city] => // [zip_code] => // [time_zone] => // [latitude] => 0 // [longitude] => 0 // [metro_code] => 0 // ) // // $geoip_info = Array( // [ip] => 118.127.46.63 // [country_code] => AU // [country_name] => Australia // [region_code] => QLD // [region_name] => Queensland // [city] => Brisbane // [zip_code] => 4000 // [time_zone] => Australia/Brisbane // [latitude] => -27.485 // [longitude] => 153.02 // [metro_code] => 0 // ) // // ------------------------------------------------------------------------- //\greatKiwi_byFernTec_adSwapper_local_v0x1x211_testDebug\pr( $geoip_info , '$geoip_info' ) ; // ========================================================================= // SUCCESS! // ========================================================================= return $geoip_info ; // ========================================================================= // That's that! // ========================================================================= } // ============================================================================= // That's that! // =============================================================================