Navigation

PHP

PHP SDK

Learn how to generate images and PDFs with the bannerify/bannerify Composer package.

bannerify/bannerify is the Bannerify client for PHP 8.1 and later. It uses Guzzle and returns errors as values, so a failed request does not throw.

Install

composer require bannerify/bannerify

Create a client

You need an API key. Create one on the API Keys page of the dashboard and keep it on the server.

<?php

require 'vendor/autoload.php';

use Bannerify\Client;

$client = new Client(getenv('BANNERIFY_API_KEY'));

Generate an image

$result = $client->createImage('tpl_xxx', [
    'modifications' => [
        ['name' => 'headline', 'text' => 'Summer sale'],
    ],
]);

if (isset($result['error'])) {
    throw new RuntimeException($result['error']['message']);
}

file_put_contents('banner.png', $result['result']);

$result['result'] is a string that holds the image bytes.

Methods

Method Result Use it to
createImage Bytes Get the image bytes
createPdf Bytes Get a PDF
createStoredImage URL string Get a hosted image URL
renderVideo (Beta) Generator of events Render a video and follow its progress
renderAndWaitVideo (Beta) URL string Render a video and get its MP4 URL
createVideo (Beta) Created video array Start a video and get its id at once, without waiting
getVideo (Beta) Video array Read the status and the MP4 URL of a video by its id
watchVideo (Beta) Generator of events Follow the progress of a video again by its id
generateImageSignedUrl URL string Build a link that renders when someone opens it

Create images and PDFs covers the first three, Render videos covers the video methods, and Signed URL covers the last one.

Handle errors

Every method except generateImageSignedUrl returns an array with a result key or an error key, and never both.

['result' => '...']

['error' => ['code' => 'NOT_FOUND', 'message' => '...', 'docs' => '...', 'requestId' => '...']]

An error from the API has all four keys. docs links to the page for that code, and you send requestId to support when you need help with a request. Errors lists the codes.

The SDK creates a few errors itself. They have code, message, and docs, but no requestId.

Code Meaning
INVALID_FORMAT The format is not png, jpeg, or webp
REQUEST_ERROR The request failed before the API answered, for example when the connection dropped or the request ran past the timeout
HTTP_ERROR The API answered with an error that the SDK could not read
TIMEOUT_ERROR A video stream dropped and the SDK could not read the video again
EXCEPTION Any other exception inside the SDK. message holds its text

generateImageSignedUrl returns a string and throws InvalidArgumentException when the format is not supported.

Options

$client = new Client(getenv('BANNERIFY_API_KEY'), [
    'timeout' => 30,
]);
Option Default Description
baseUrl https://api.bannerify.co/v1 The API base URL
timeout 60.0 Request timeout in seconds

Learn more