GithubHelp home page GithubHelp logo

fanrong33 / ip2location-c-library Goto Github PK

View Code? Open in Web Editor NEW

This project forked from chrislim2888/ip2location-c-library

0.0 1.0 0.0 6.83 MB

IP2Location C library enables the user to find the country, region, city, coordinates, zip code, time zone, ISP, domain name, connection type, area code, weather and mobile information that any IP address or hostname originates from.

Home Page: http://www.ip2location.com

License: GNU Lesser General Public License v3.0

Makefile 18.56% Shell 65.69% M4 0.46% Perl 6.06% C 9.22%

ip2location-c-library's Introduction

IP2Location C Library

IP2Location is a C library that enables the user to find the country, region, city, latitude, longitude, zip code, time zone, ISP, domain name, connection type, area code, weather, mobile network, elevation, usage type by IP address or hostname originates from. The library reads the geo location information from IP2Location BIN data file.

Supported IPv4 and IPv6 address.

For more details, please visit: http://www.ip2location.com/developers/c

Installation

Unix/Linux

autoreconf -i -v --force
./configure
make
cd data
perl ip-country.pl

Windows

Execute "vcvarsall.bat". (This file is part of Microsoft Visual C, not ip2location code) 
nmake -f Makefile.win
cd data
perl ip-country.pl

MacOS

autoreconf -i -v --force
export CFLAGS=-I/usr/include/malloc 
./configure
make
cd data
perl ip-country.pl

Testing

cd test
test-IP2Location

Sample Code

安装路径/usr/local/bin/IP2Location-C-Library-master,设置LD_LIBRARY_PATH环境变量

vi /etc/profile
export LD_LIBRARY_PATH=/usr/local/bin/IP2Location-C-Library-master/libIP2Location/.libs:$LD_LIBRARY_PATH
source /etc/profile

将ip2location.c 放在与IP2Location-C-Library-master/目录同级,编译第一个命令是示例,使用第二个命令进行编译

gcc -o sample sample.c -I../libIP2Location -L../libIP2Location/.libs -lIP2Location
gcc -o ip2location ip2location.c -IIP2Location-C-Library-master/libIP2Location -LIP2Location-C-Library-master/libIP2Location/.libs -lIP2Location

gcc -o ip2location_ipv6 ip2location_ipv6.c -IIP2Location-C-Library-master/libIP2Location -LIP2Location-C-Library-master/libIP2Location/.libs -lIP2Location

测试

/usr/local/bin/ip2location 8.8.8.8
"US","United States","California","Mountain View","37.405991","-122.078514","Google Inc.","google.com","-","-","-","SES"

/usr/local/bin/ip2location_ipv6 2600:1005:b12c:7c89:f962:5d91:7de7:c0b1
"US","United States","Georgia","Valdosta","30.832701","-83.278488","verizonwireless.com","Cellco Partnership DBA Verizon Wireless","310","004/005/006/012","Verizon","ISP/MOB"

php接口,使用shell_exec调用c模块

/**
 * 使用ip2location c library模块进行ip查询
 * TODO 未完善安全验证,测试错误路径
 */
 public function ip_query(){
    $ip = $_GET['ip'];
    if($ip == ''){
        $this->ajaxReturn('', 'ip parameter is missing', 0);
    }

    if(strpos($ip, ':') === false){ // IPV4
        $result = shell_exec("/usr/local/bin/ip2location $ip 2>&1");
    }else{ // IPV6
        $result = shell_exec("/usr/local/bin/ip2location_ipv6 $ip 2>&1");
    }
    $key_map = array(
        'ip',
        'country_code',
        'country',
        'region',
        'city',
        'lat',
        'lon',
        'domain',
        'isp',
        'mcc',
        'mnc',
        'carrier',
        'usage_type',
        // 'timezone',
        // 'zip',
    );
    if($result){
        $result = substr(trim($result), 1, -1);
        $pieces = split('","', $result);
        foreach ($pieces as $key => $value) {
            $pieces[$key] = $value == '-' ? '' : $value;
        }
        array_unshift($pieces, $ip);


        $data = array_combine($key_map, $pieces);
        $data['timezone'] = '';
        $data['zip']      = '';
        
        $this->ajaxReturn($data, 'success', 1);
    }else{
        $this->ajaxReturn(array(), 'fail', 0);
    }
}

php api result

{"data":{"ip":"121.23.202.33","country_code":"CN","country":"China","region":"Zhejiang","city":"Hangzhou","lat":"30.293650","lon":"120.161423","domain":"aliyun.com","isp":"Aliyun Computing Co. Ltd","mcc":"","mnc":"","carrier":"","usage_type":"DCH","timezone":"","zip":""},"info":"success","status":1}

FAQ

  1. AC_PROG_LIBTOOL错误
# autoreconf -i -v --force
configure.ac:42: error: possibly undefined macro: AC_PROG_LIBTOOL
# yum install libtool
  1. 有时候后台命令行执行可以,但是在PHP执行却出现以下错误

/usr/local/bin/ip2location: error while loading shared libraries: libIP2Location.so.1: cannot open shared object file: No such file or directory

或者是php执行可以,但是通过cron来执行php中的代码却出现没有返回任何数据的bug

经过搜索资料,发现部分linux系统存有的通病。要解决这个方法非常容易

如果是32位系统

ln -s /usr/local/bin/IP2Location-C-Library/libIP2Location/.libs/libIP2Location.so.1 /lib

如果是64位系统

ln -s /usr/local/bin/IP2Location-C-Library/libIP2Location/.libs/libIP2Location.so.1 /lib64

Sample BIN Databases

Support

Email: [email protected].
URL: http://www.ip2location.com

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.