Skip to main content

VIUCraft Documentation

PHP Integration

On This Page

Integrate VIUCraft with your PHP applications.

Using cURL

PHP
function uploadImage(string $filePath): array
{
    $apiKey = getenv(VIUCRAFT_API_KEY);

    $ch = curl_init();

    curl_setopt_array($ch, [
        CURLOPT_URL => https://api.viucraft.com/upload,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POST => true,
        CURLOPT_HTTPHEADER => [
            X-API-Key:  . $apiKey,
        ],
        CURLOPT_POSTFIELDS => [
            image => new CURLFile($filePath),
        ],
    ]);

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

    return json_decode($response, true);
}

Using Guzzle

PHP
use GuzzleHttp\\Client;

class VIUCraftClient
{
    private Client $client;

    public function __construct(string $apiKey)
    {
        $this->client = new Client([
            base_uri => https://api.viucraft.com/,
            headers => [X-API-Key => $apiKey],
        ]);
    }

    public function upload(string $filePath): array
    {
        $response = $this->client->post(upload, [
            multipart => [[
                name => image,
                contents => fopen($filePath, r),
            ]],
        ]);

        return json_decode($response->getBody()->getContents(), true);
    }
}

Was this helpful?

On This Page