提供地理服务相关接口。
通过Composer安装
composer require fu/geo 2.*
将下面的代码加入composer.json
{
"require": {
"fu/geo": "2.*"
}
}
> vendor/bin/phpcs --standard=PSR12 src
> vendor/bin/phpcbf --standard=PSR12 src
> vendor/bin/phpunit
Generating code coverage report in HTML format ... done [00:00.020]
include "./vendor/autoload.php";
use Fu\Geo\GeoCoder;
use Fu\Geo\Service\Coordinary\TencentCoordinaryLocationService;
const KEY = "YOUR-TENCENT-GEO-SERVICE-KEY";
$latitude = 29.60001;
$longitude = 91.00001;
$service = new TencentCoordinaryLocationService(KEY);
$response = $service->getLocation($latitude, $longitude);
if ($response->isOk()) {
$area = $response->getArea();
$area->nation; // 中国
$area->lv1; // 西藏自治区
$area->lv2; // 拉萨市
$area->lv3; // 堆龙德庆区
} else {
// get the response raw data...
var_dump($response->getRaw());
}
include "./vendor/autoload.php";
use Fu\Geo\IpCoder;
use Fu\Geo\Service\Ip\TencentIpLocationService;
const KEY = "YOUR-TENCENT-GEO-SERVICE-KEY";
$ip = '171.221.208.34';
$service = new TencentIpLocationService(KEY);
$response = $service->getLocation($ip);
if ($response->isOk()) {
$area = $response->getArea()
$area->nation; // 中国
$area->lv1; // 四川省
$area->lv2; // 成都市
$area->lv3; // 温江区
} else {
// get the response raw data...
var_dump($response->getRaw());
}
include "./vendor/autoload.php";
use Fu\Geo\IpCoder;
use Fu\Geo\Service\Phone\AliPhoneLocationService;
const KEY = "YOUR-ALI-SERVICE-KEY";
$areaCode = '86';
$phone = '13880799123';
$service = new AliPhoneLocationService(KEY);
$response = $service->getLocation($areaCode, $phone);
if ($response->isOk()) {
$area = $response->getArea()
$area->nation; // 中国
$area->lv1; // 四川省
$area->lv2; // 成都市
$area->lv3; // 温江区
} else {
// get the response raw data...
var_dump($response->getRaw());
}
- 腾讯地理位置服务:https://lbs.qq.com
- 阿里云市场手机归属地查询服务
The code in this project is licensed under MIT license.