Search by

weblabormx / world

weblabormx

There is no license information available for the latest version (1.0.8) of this package.

Weblabor World API Wrapper Library for PHP

Package info

gitlab.weblabor.mx/weblabormx/paquetes/world

pkg:composer/weblabormx/world

Statistics

Installs: 4 726

Dependents: 1

Suggesters: 0

1.0.8 2026-10-13 18:38 UTC

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.

Request timeout

Every request waits up to 30 seconds by default. Change that default for every call made through the static client, including all the Division endpoints, with World::setTimeout (in seconds). It works before or after World::init, and World::getTimeout() reads it back:

World::setTimeout(3);
World::init(apiKey: "YOUR_API_KEY");

A client you build yourself takes it as timeout in its constructor:

$client = new Client(apiKey: 'YOUR_API_KEY', timeout: 3);

Pass timeout (in seconds) to makeCall or makeSafeCall to override the default on a single request:

$country = $client->makeSafeCall('/country/MX', timeout: 3);

When the request fails, makeCall throws an exception whose message includes the cause, such as the timeout reached, and makeSafeCall returns null. An HTTP error status (4xx or 5xx) counts as a failure.

Telling failures apart

request takes the same arguments as makeCall and never throws. It returns a WeblaborMx\World\Response that says how the request ended:

$response = $client->request('/division/3576390/children');

if ($response->ok()) {
    $rows = $response->data; // an empty array is a successful answer with no rows
} elseif ($response->rateLimited()) {
    $seconds = $response->retryAfter; // from the Retry-After header, or null
}

failure is null on success, or a case of the WeblaborMx\World\Failure enum:

failureWhen
nullThe service answered a JSON array or object; data holds it, [] included
Failure::RateLimited (rate_limited)The service answered 429 Too Many Requests
Failure::Http (http)Any other 4xx or 5xx status
Failure::Invalid (invalid)The body is not JSON, or not the list of divisions that was asked for
Failure::Network (network)The request did not complete: no connection, a timeout, a DNS error
Failure::Unconfigured (unconfigured)World::init was never called (World::hasClient() is false)

status holds the HTTP status when there was one, retryAfter the seconds the service asked to wait (sent as a number or as an HTTP date) and error a message with the cause.

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();

When you need to tell a division without children from a request that failed, use childrenOf, which answers a Response whose data holds the Division objects:

$response = Division::childrenOf(3576390, ['id', 'name']);

$response->ok() && $response->data === []; // the division has no children

children() and Division::getChildren() answer [] for a division without children and null when the request failed.

Division::allCountries() does the same for the list of countries.

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::setTimeout(3); # Optionally wait less than 30 seconds per request
    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=