112 lines
4.6 KiB
Markdown
112 lines
4.6 KiB
Markdown
# MozOut Mozambique API SDK for PHP
|
|
|
|
<!-- badges:start -->
|
|
[](https://github.com/mozout-mz/api-php-sdk/actions/workflows/ci.yml) [](https://github.com/mozout-mz/api-php-sdk/releases) [](LICENSE)
|
|
<!-- badges:end -->
|
|
|
|
A PHP library for the MozOut Mozambique API: domains, DNS, SMS, E-mail Center, AI, billing, support, servers and the rest. It covers all 256 operations of the API, one method per operation, and it is generated from the OpenAPI specification, so it keeps up with the API.
|
|
|
|
[Português](README.pt.md) · [All operations](OPERATIONS.md)
|
|
|
|
## Install
|
|
|
|
```bash
|
|
composer require mozout-mz/sdk
|
|
```
|
|
|
|
Needs PHP 7.4 or newer with the curl and json extensions. Without Composer, `require` the `autoload.php` of this package.
|
|
|
|
## Get started
|
|
|
|
Create a key in the [API Center](https://www.meu.mozout.co.mz/api-center.php) of Meu MozOut (docs: https://docs.mozout.co.mz). Start with a `sandbox_` key: it reads your real data, but writes are simulated and never charge. Pass the key in the code or in an environment variable: `MOZOUT_API_KEY`.
|
|
|
|
```php
|
|
use MozOutMz\Client;
|
|
|
|
$client = new Client(); // or new Client(['api_key' => 'sandbox_...'])
|
|
echo $client->status->pingGet()['data']['status'];
|
|
```
|
|
|
|
The SDK already points to the MozOut Mozambique API: `https://api.mozout.co.mz`.
|
|
|
|
Options: `new Client(['api_key' => ..., 'brand' => ..., 'base_url' => ..., 'language' => 'pt', 'mode' => 'sandbox', 'timeout' => 60, 'max_retries' => 2])`.
|
|
|
|
The areas are `status`, `token`, `actions`, `specification`, `sms`, `emailCenter`, `account`, `catalog`, `domains`, `whois`, `services`, `billing`, `support`, `dns`, `servers`, `hosting`, `email`, `extras`, `reseller`, `ai`. For example, `GET /v1/domains/{domain}` is `domains.get`. The full list is in [OPERATIONS.md](OPERATIONS.md).
|
|
|
|
## Examples
|
|
|
|
Register a domain:
|
|
|
|
```php
|
|
$check = $client->domains->availability(['domain' => 'example.co.mz']);
|
|
if (!empty($check['data']['available'])) {
|
|
$client->domains->register(['domain' => 'example.co.mz', 'years' => 1]);
|
|
}
|
|
```
|
|
|
|
Send an SMS:
|
|
|
|
```php
|
|
$client->sms->messagesCreate(['number' => '+258841234567', 'text' => 'Your order is ready.']);
|
|
```
|
|
|
|
Send an e-mail with the E-mail Center:
|
|
|
|
```php
|
|
$client->emailCenter->messagesCreate([
|
|
'from' => 'Example <notifications@example.com>',
|
|
'to' => ['ana@example.org'],
|
|
'subject' => 'Your order is ready',
|
|
'text' => 'Hello Ana, your order is ready to collect.',
|
|
]);
|
|
```
|
|
|
|
Chat with the AI:
|
|
|
|
```php
|
|
$answer = $client->ai->chatCreate([
|
|
'model' => 'deepseek-ai/deepseek-v4-flash',
|
|
'messages' => [['role' => 'user', 'content' => 'Write a welcome message for our website.']],
|
|
]);
|
|
echo $answer['choices'][0]['message']['content'];
|
|
|
|
// With 'stream' => true and an on_event callable, each event goes to the callable
|
|
$client->ai->chatCreate(['model' => 'deepseek-ai/deepseek-v4-flash', 'messages' => [['role' => 'user', 'content' => 'Hello']], 'stream' => true],
|
|
['on_event' => function ($event) { print_r($event); }]);
|
|
```
|
|
|
|
Complete examples are in the `examples/` folder.
|
|
|
|
## Errors, retries and Idempotency-Key
|
|
|
|
When the API answers with an error, the SDK throws an exception with the HTTP status, the `code` (it never changes), the message and the `correlation_id` to give to support. The most common codes: `401 unauthorized` (wrong or expired key), `402 insufficient_credit` (not enough balance), `403 insufficient_scope` (the key lacks the permission) and `429 rate_limited`.
|
|
|
|
```php
|
|
use MozOutMz\ApiException;
|
|
|
|
try {
|
|
$client->sms->messagesCreate(['number' => '+258841234567', 'text' => 'Hello']);
|
|
} catch (ApiException $e) {
|
|
echo $e->getStatus(), ' ', $e->getApiCode(), ' ', $e->getMessage(), ' ', $e->getCorrelationId(); // e.g. 402 insufficient_credit
|
|
}
|
|
```
|
|
|
|
On `429` and `5xx` the SDK waits and tries again, up to 2 times by default (it follows `Retry-After`). On operations that accept an `Idempotency-Key`, the SDK creates one on its own and repeats it on every retry, so a request is never done twice. You can pass your own: `['idempotency_key' => '...']`.
|
|
|
|
The status and headers of the last answer are in `$client->lastResponse`, for example to know whether a write was simulated (`X-API-Simulated`).
|
|
|
|
## Webhooks
|
|
|
|
Account events arrive signed in the `X-Webhook-Signature` header. To check the signature:
|
|
|
|
```php
|
|
$ok = \MozOutMz\Webhook::verify(file_get_contents('php://input'), $_SERVER['HTTP_X_WEBHOOK_SIGNATURE'] ?? '', $secret);
|
|
```
|
|
|
|
## Version
|
|
|
|
Version 1.0.0, generated from version 1.0.0 of the API specification.
|
|
|
|
---
|
|
|
|
Maintained by RLX Holdings.
|