增加依赖和utils,实现ip位置查询

This commit is contained in:
Chenx221 2024-03-01 18:01:34 +08:00
parent 5579936e0a
commit 5f6dd996ed
Signed by: chenx221
GPG Key ID: D7A9EC07024C3021
2 changed files with 38 additions and 1 deletions

View File

@ -43,7 +43,8 @@
"ramsey/uuid": "^4.7", "ramsey/uuid": "^4.7",
"google/recaptcha": "^1.3", "google/recaptcha": "^1.3",
"vlucas/phpdotenv": "^5.6", "vlucas/phpdotenv": "^5.6",
"yiisoft/yii2-httpclient": "^2.0" "yiisoft/yii2-httpclient": "^2.0",
"ipinfo/ipinfo": "^3.1"
}, },
"require-dev": { "require-dev": {
"yiisoft/yii2-debug": "~2.1.0", "yiisoft/yii2-debug": "~2.1.0",

36
utils/IPLocation.php Normal file
View File

@ -0,0 +1,36 @@
<?php
namespace app\utils;
use ipinfo\ipinfo\Details;
use ipinfo\ipinfo\IPinfo;
use ipinfo\ipinfo\IPinfoException;
use Yii;
class IPLocation
{
private IPinfo $client;
public function __construct()
{
$this->client = new IPinfo(Yii::$app->params['ipinfoToken']);
}
public static function getDetails(string $ip): ?Details
{
$instance = new self();
try {
return $instance->client->getDetails($ip);
} catch (IPinfoException $e) {
Yii::error($e->getMessage());
/*
* Note:
* 如果出现SSL certificate problem: unable to get local issuer certificate
* 下载 https://curl.haxx.se/ca/cacert.pem php\extras\ssl 并在php.ini中配置
* curl.cainfo = "文件的绝对路径"
* 解决方法参考: https://martinsblog.dk/windows-iis-with-php-curl-60-ssl-certificate-problem-unable-to-get-local-issuer-certificate/
*/
return null;
}
}
}