├── .gitignore ├── LICENSE ├── README.md ├── _config.yml ├── composer.json ├── phpunit.xml ├── src └── IpService.php └── tests ├── Ip2LocaleTest.php └── TestsBase.php /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | composer.lock -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Dimitris Savvopoulos 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Laravel IP Service 2 | ==================== 3 | 4 | Tries to guess the country code of the client, using his IP. 5 | 6 | ## Installation 7 | 8 | 9 | [Download](http://ip2nation.com/ip2nation/Download) and import the ip database from [ip2nation.com](http://ip2nation.com/) 10 | 11 | ## Usage 12 | 13 | ```php 14 | $service = App::make('Dimsav\IpService\IpService'); 15 | 16 | 17 | // country code for the given ip address 18 | 19 | echo $service->getCountryCodeFromIp('123.123.123.123'); 20 | 21 | 22 | // country code for the client's ip address 23 | 24 | echo $service->getCountryCodeFromClientIp(); 25 | ``` 26 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-leap-day -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "dimsav/laravel-ip-service", 3 | "description": "A Laravel package to get the client's country using the ip. A wrapper for http://ip2nation.com", 4 | "keywords": ["laravel", "ip", "country", "nation"], 5 | "license": "MIT", 6 | "authors": [ 7 | { 8 | "name": "Dimitris Savvopoulos", 9 | "email": "ds@dimsav.com", 10 | "homepage": "http://dimsav.com" 11 | } 12 | ], 13 | "require": { 14 | "php": ">=5.4.0", 15 | "illuminate/support": "~4.0" 16 | }, 17 | "require-dev": { 18 | "orchestra/testbench": "~2.0", 19 | "phpunit/phpunit": "~4.0", 20 | "mockery/mockery": "~0.9" 21 | }, 22 | "autoload": { 23 | "psr-4": { 24 | "Dimsav\\IpService\\": "src/" 25 | }, 26 | "classmap": [ 27 | "tests" 28 | ] 29 | } 30 | } -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | ./tests/ 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /src/IpService.php: -------------------------------------------------------------------------------- 1 | config = $config; 12 | $this->request = $request; 13 | } 14 | 15 | /** 16 | * @return string|null 17 | */ 18 | public function getCountryCodeFromClientIp() 19 | { 20 | $ip = $this->request->ip(); 21 | return $this->getCountryCodeFromIp($ip); 22 | } 23 | 24 | /** 25 | * @param $ip 26 | * @return string|null 27 | */ 28 | public function getCountryCodeFromIp($ip) 29 | { 30 | $result = DB::select('SELECT 31 | c.code 32 | FROM 33 | ip2nationCountries c, 34 | ip2nation i 35 | WHERE 36 | i.ip < INET_ATON(?) 37 | AND 38 | c.code = i.country 39 | ORDER BY 40 | i.ip DESC 41 | LIMIT 0,1', [$ip]); 42 | 43 | if ($row = array_pop($result)) 44 | { 45 | return $row->code; 46 | } 47 | return null; 48 | } 49 | 50 | } -------------------------------------------------------------------------------- /tests/Ip2LocaleTest.php: -------------------------------------------------------------------------------- 1 | once()->andReturnValues([ 17 | $greekIp, 18 | $germanIp, 19 | $cyprusIp 20 | ]); 21 | 22 | $service = $this->getService(); 23 | $this->assertSame('gr', $service->getCountryCodeFromClientIp()); 24 | $this->assertSame('de', $service->getCountryCodeFromClientIp()); 25 | $this->assertSame('cy', $service->getCountryCodeFromClientIp()); 26 | } 27 | 28 | /** 29 | * @return IpService 30 | */ 31 | private function getService() 32 | { 33 | return App::make('Dimsav\IpService\IpService'); 34 | } 35 | } 36 | 37 | -------------------------------------------------------------------------------- /tests/TestsBase.php: -------------------------------------------------------------------------------- 1 | set('database.default', 'mysql'); 13 | $app['config']->set('database.connections.mysql', array( 14 | 'driver' => 'mysql', 15 | 'host' => 'localhost', 16 | 'database' => 'test_ipservice', 17 | 'username' => 'homestead', 18 | 'password' => 'secret', 19 | 'charset' => 'utf8', 20 | 'collation' => 'utf8_unicode_ci', 21 | )); 22 | } 23 | 24 | } 25 | --------------------------------------------------------------------------------