weblabormx/world
Composer 安装命令:
composer require weblabormx/world
包简介
Weblabor World API Wrapper Library for PHP
README 文档
README
This is a PHP library to work with the World API.
Requirements
- PHP 8.1 and later.
Packagist
Install via packagist using:
composer require weblabormx/world
Don't forget to include the bindings:
require_once __DIR__ . '/vendor/autoload.php';
Initialize credentials
You can choose to work with the library using the World static class like this:
use WeblaborMx\World\World; // World::setApiBase("https://world.weblabor/api"); # Optionally set an alternative base World::init(apiKey: "YOUR_API_KEY"); $client = World::getClient(); // Do stuff...
Or by instantiating a client object:
use WeblaborMx\World\Client; $client = new Client( apiKey: 'YOUR_API_KEY', // apiBase: 'https://world.weblabor/api', ); // Do stuff...
The static way is recommended.
Divisions
The API works by getting the different levels of divisions of the world. Yoy may normally know them as countries, states and cities, but other nations call them different and have more than 3 levels.
So, the world is divided in different ways depending each different sovereign nation.
We normally call, the sovereign nations, the upmost level of division, countries. The children of the countries, states. And the children of those states, cities. This is the way the API is used, by traversing the different levels of divisions. A division has children() and parent() unless it's the upmost or downmost level of division.
Properties
All calls from the division endpoints, returns a WeblaborMx\World\Entities\Division object or array.
You can check the properties by inspecting the class constructor:
public function __construct( public int $id, public ?string $name = null, public ?string $country = null, public ?string $a1code = null, public ?string $level = null, public ?int $population = null, public ?float $lat = null, public ?float $long = null, public ?string $timezone = null, public ?int $parent_id = null, ) { }
Endpoints
Check all endpoints in the documentation.
Obtaining all countries
You probably want to start fetching divisions by their upmost level.
use WeblaborMx\World\World; use WeblaborMx\World\Entities\Division; $client = World::getClient(); /** @var Division[] **/ $countries = $client->makeCall('/countries'); foreach ($countries as $division) { echo $division->name . \PHP_EOL; }
Get country by code
You can search a country by its ISO-3166 code.
use WeblaborMx\World\World; use WeblaborMx\World\Entities\Division; $client = World::getClient(); $code = "MX"; /** @var Division|null **/ $country = $client->makeCall("/country/{$code}");
Getting a specific division
To get a specific division, you pass its previously obtained ID.
use WeblaborMx\World\Entities\Division; $id = 3531011; // Probably want to obtain it from the DB /** @var Division|null **/ $division = Division::get($id);
Obtaining all children
/** @var Division[] **/ $children = $division->children();
Obtaining parent
/** @var Division|null **/ $parent = $division->parent();
Working with Laravel
To maintain the library lightweight, no dependency was added. However, you can find a Division casting class, that although it doesn't implements the contract from Laravel, it should work as any other cast.
With the cast you can save any ID obtained in a model, and automatically obtain a Division when accesing the property again.
use Illuminate\Database\Eloquent\Model; use WeblaborMx\World\Casts\DivisionCast; class Company extends Model { protected $casts = [ 'country' => DivisionCast::class, 'state' => DivisionCast::class, ]; // ... }
Registering client
You probably want to initialize the client through a service provider like AppServiceProvider.
public function boot(): void { // World::setApiBase(config('services.weblabor.world.endpoint')); World::init(apiKey: config('services.weblabor.world.token')); }
Then setup the services.php configuration.
return [ // ... 'weblabor' => [ 'world' => [ 'endpoint' => env('WEBLABOR_WORLD_ENDPOINT', 'https://world.weblabor.mx/api'), 'token' => env('WEBLABOR_WORLD_TOKEN'), ] ] ]
And the credentials in .env.
WEBLABOR_WORLD_ENDPOINT=
WEBLABOR_WORLD_TOKEN=
weblabormx/world 适用场景与选型建议
weblabormx/world 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.83k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 12 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 weblabormx/world 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 weblabormx/world 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 3.83k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 3
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2023-12-26