Skip to main content

PHP Examples — UnicyFalcon API

This guide shows how to integrate the UnicyFalcon API in PHP.
API access requires the API plan or higher.

1) Reusable PHP API client

<?php

class UnicyFalconApiClient
{
    private string $apiKey;
    private string $hmacSecret;
    private string $baseUrl;
    private int $timeout;

    public function __construct(string $apiKey, string $hmacSecret, string $baseUrl, int $timeout = 30)
    {
        $this->apiKey = $apiKey;
        $this->hmacSecret = $hmacSecret;
        $this->baseUrl = rtrim($baseUrl, '/');
        $this->timeout = $timeout;
    }

    private function sign(string $method, string $uri, string $body, int $timestamp): string
    {
        $payload = strtoupper($method) . '|' . $uri . '|' . $body . '|' . $timestamp;
        return base64_encode(hash_hmac('sha256', $payload, $this->hmacSecret, true));
    }

    public function request(string $method, string $endpoint, array $data = []): array
    {
        $url = $this->baseUrl . '/' . ltrim($endpoint, '/');
        $timestamp = time();
        $body = !empty($data) ? json_encode($data) : '';

        $parsedUrl = parse_url($this->baseUrl);
        $basePath = $parsedUrl['path'] ?? '';
        $uri = $basePath . '/' . ltrim($endpoint, '/');

        $signature = $this->sign($method, $uri, $body, $timestamp);

        $headers = [
            'Content-Type: application/json',
            'Accept: application/json',
            'X-API-Key: ' . $this->apiKey,
            'X-Timestamp: ' . $timestamp,
            'X-Signature: ' . $signature,
        ];

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, strtoupper($method));

        if (!empty($body)) {
            curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
        }

        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $error = curl_error($ch);
        curl_close($ch);

        if ($error) {
            throw new Exception("cURL Error: {$error}");
        }

        $decoded = json_decode($response, true);
        if ($httpCode >= 400) {
            $message = $decoded['message'] ?? 'Unknown error';
            throw new Exception("API Error (HTTP {$httpCode}): {$message}");
        }

        return $decoded;
    }

    public function get(string $endpoint): array { return $this->request('GET', $endpoint); }
    public function post(string $endpoint, array $data): array { return $this->request('POST', $endpoint, $data); }
    public function put(string $endpoint, array $data): array { return $this->request('PUT', $endpoint, $data); }
    public function delete(string $endpoint): array { return $this->request('DELETE', $endpoint); }
}

2) Usage examples

<?php

require_once 'UnicyFalconApiClient.php';

$client = new UnicyFalconApiClient(
    apiKey: 'ak_your_api_key_here',
    hmacSecret: 'your_hmac_secret_here',
    baseUrl: 'https://your-subdomain.unicyfalcon.com/api/v1'
);

// List customers
$customers = $client->get('/customers');

// Create an order
$order = $client->post('/orders', [
    'customer_id' => 'uuid',
    'order_number' => 'ORD-2026-001',
    'order_items' => [
        ['product_id' => 'uuid', 'quantity' => 2, 'unit_price' => 99.99]
    ],
    'status' => 'pending',
    'pickup_address_id' => 'uuid',
    'dropoff_address_id' => 'uuid'
]);