Skip to content

Instantly share code, notes, and snippets.

@zacscott
Last active August 29, 2015 14:22
Show Gist options
  • Save zacscott/1eaea28811a2a62e71b8 to your computer and use it in GitHub Desktop.
Save zacscott/1eaea28811a2a62e71b8 to your computer and use it in GitHub Desktop.
GeoIP.php
<?php
/** GeoIP.php - GeoIP API lookup interface
* Uses the Free GeoIP API to perform lookup - http://freegeoip.net
*/
// dont double include
if ( class_exists( 'GeoIP' ) ) {
return;
}
/**
* GeoIP API lookup interface.
* Uses the Free GeoIP API to perform lookup - http://freegeoip.net
*
* @author Zachary Scott <[email protected]>
*/
class GeoIP {
// The GeoIP lookup API
const API_URL = 'http://freegeoip.net/json/';
public function __construct() { }
/**
* Performs a GeoIP lookup for the given IP.
*
* @param $ip string The IP to perform the lookup on. If empty, will use the
* remote client.
* @return The GeoIP data, `null` if failed.
*/
public function lookup( $ip = null ) {
// If no IP given, default to remote client
if ( empty( $ip ) ) {
$ip = isset( $_SERVER['REMOTE_ADDR'] ) ? $_SERVER['REMOTE_ADDR'] : '';
$ip = trim( $ip );
// fallback to hostname
if ( empty( $ip ) ) {
$ip = isset( $_SERVER['REMOTE_HOST'] ) ? $_SERVER['REMOTE_HOST'] : '';
$ip = trim( $ip );
}
}
// Do GeoIP API lookup
$http_resp = wp_safe_remote_get( self::API_URL . $ip );
if ( is_wp_error( $http_resp ) ) {
return false;
}
// Parse the response
$json_resp = $http_resp['body'];
$resp = json_decode( $json_resp, true );
if ( empty( $resp ) ) {
return false;
}
$result = array(
'ip' => isset( $resp['ip'] ) ? $resp['ip'] : $ip,
'country' => isset( $resp['country_name'] ) ? $resp['country_name'] : '',
'country_code' => isset( $resp['country_code'] ) ? $resp['country_code'] : '',
'region_code' => isset( $resp['region_code'] ) ? $resp['region_code'] : ''
);
return $result;
}
/** Pings the API are returns whenther it is working or not. */
public function ping() {
$resp = $this->lookup( '8.8.8.8' ); // Google DNS server IP
return ! empty( $resp );
}
}
/** Copyright (c) 2014, 2015 <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment