-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 5e2c4b9
Showing
3 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace daxslab\ipstack; | ||
|
||
use Yii; | ||
use yii\base\Component; | ||
use yii\base\InvalidArgumentException; | ||
|
||
/** | ||
* This is just an example. | ||
*/ | ||
class Client extends Component | ||
{ | ||
public $api_key = null; | ||
|
||
protected $base_url = 'http://api.ipstack.com/{ip}?access_key={key}'; | ||
|
||
public function init() | ||
{ | ||
parent::init(); | ||
if ($this->api_key === null) { | ||
throw new InvalidArgumentException(Yii::t('app', 'API key can\'t be null.')); | ||
} | ||
} | ||
|
||
public function getData($ip = null) | ||
{ | ||
$searchIp = $ip ?? \Yii::$app->request->userIP; | ||
try { | ||
$url = str_replace(['{ip}', '{key}'], [$searchIp, $this->api_key], $this->base_url); | ||
return json_decode(file_get_contents($url), true); | ||
} catch (\Exception $e) { | ||
Yii::error(serialize([ | ||
'message' => $e->getMessage(), | ||
'ip' => $searchIp, | ||
]), __METHOD__); | ||
return null; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
Yii2 IPStack Client | ||
=================== | ||
Locate and identify website visitors by IP address using IPStack API | ||
|
||
Installation | ||
------------ | ||
|
||
The preferred way to install this extension is through [composer](http://getcomposer.org/download/). | ||
|
||
Either run | ||
|
||
``` | ||
php composer.phar require --prefer-dist daxslab/yii2-ipstack "*" | ||
``` | ||
|
||
or add | ||
|
||
``` | ||
"daxslab/yii2-ipstack": "*" | ||
``` | ||
|
||
to the require section of your `composer.json` file. | ||
|
||
|
||
Configuration | ||
------------- | ||
|
||
Add the component to the application: | ||
|
||
```php | ||
'components' => [ | ||
'ipStack' => [ | ||
'class' => \daxslab\ipstack\Client::class, | ||
'api_key' => '{YOUR_API_KEY}', | ||
], | ||
] | ||
``` | ||
|
||
Usage | ||
----- | ||
|
||
Request info about current user IP | ||
|
||
```php | ||
Yii::$app->ipStack->getData(); | ||
``` | ||
|
||
Request info about a custom IP | ||
|
||
```php | ||
Yii::$app->ipStack->getData($ip); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "daxslab/yii2-ipstack", | ||
"description": "Locate and identify website visitors by IP address ", | ||
"type": "yii2-extension", | ||
"keywords": ["yii2","extension"," ip"," ipstack"," geo"," geolocalization"], | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Daxslab", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"yiisoft/yii2": "~2.0.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"daxslab\\ipstack\\": "" | ||
} | ||
} | ||
} |