Skip to content

dubinc/dub-php

Repository files navigation

dub/dub-php

Summary

Dub.co API: Dub is link management infrastructure for companies to create marketing campaigns, link sharing features, and referral programs.

Table of Contents

SDK Installation

The SDK relies on Composer to manage its dependencies.

To install the SDK and add it as a dependency to an existing composer.json file:

composer require "dub/dub-php"

SDK Example Usage

Example 1

declare(strict_types=1);

require 'vendor/autoload.php';

use Dub;
use Dub\Models\Operations;

$security = 'DUB_API_KEY';

$sdk = Dub\Dub::builder()->setSecurity($security)->build();

$request = new Operations\CreateLinkRequestBody(
    url: 'https://google.com',
    tagIds: [
        'clux0rgak00011...',
    ],
    externalId: '123456',
);

$response = $sdk->links->create(
    request: $request
);

if ($response->linkSchema !== null) {
    // handle response
}

Example 2

declare(strict_types=1);

require 'vendor/autoload.php';

use Dub;
use Dub\Models\Operations;

$security = 'DUB_API_KEY';

$sdk = Dub\Dub::builder()->setSecurity($security)->build();

$request = new Operations\UpsertLinkRequestBody(
    url: 'https://google.com',
    tagIds: [
        'clux0rgak00011...',
    ],
    externalId: '123456',
);

$response = $sdk->links->upsert(
    request: $request
);

if ($response->linkSchema !== null) {
    // handle response
}

Available Resources and Operations

Available methods
  • retrieve - Retrieve analytics for a link, a domain, or the authenticated workspace.
  • create - Create a domain
  • list - Retrieve a list of domains
  • update - Update a domain
  • delete - Delete a domain
  • list - Retrieve a list of events
  • get - Retrieve the metatags for a URL
  • get - Retrieve a QR code
  • get - Retrieve a workspace
  • update - Update a workspace

Error Handling

Handling errors in this SDK should largely match your expectations. All operations return a response object or throw an exception.

By default an API error will raise a Errors\SDKException exception, which has the following properties:

Property Type Description
$message string The error message
$statusCode int The HTTP status code
$rawResponse ?\Psr\Http\Message\ResponseInterface The raw HTTP response
$body string The response content

When custom error responses are specified for an operation, the SDK may also throw their associated exception. You can refer to respective Errors tables in SDK docs for more details on possible exception types for each operation. For example, the create method throws the following exceptions:

Error Object Status Code Content Type
Errors\BadRequest 400 application/json
Errors\Unauthorized 401 application/json
Errors\Forbidden 403 application/json
Errors\NotFound 404 application/json
Errors\Conflict 409 application/json
Errors\InviteExpired 410 application/json
Errors\UnprocessableEntity 422 application/json
Errors\RateLimitExceeded 429 application/json
Errors\InternalServerError 500 application/json
Dub\Models\Errors.SDKException 4xx-5xx /

Example

declare(strict_types=1);

require 'vendor/autoload.php';

use Dub;
use Dub\Models\Operations;

$security = 'DUB_API_KEY';

$sdk = Dub\Dub::builder()->setSecurity($security)->build();

try {
    $request = new Operations\CreateLinkRequestBody(
        url: 'https://google.com',
        tagIds: [
            'clux0rgak00011...',
        ],
        externalId: '123456',
    );

    $response = $sdk->links->create(
        request: $request
    );

    if ($response->linkSchema !== null) {
        // handle response
    }
} catch (Errors\BadRequestThrowable $e) {
    // handle $e->$container data
    throw $e;
} catch (Errors\UnauthorizedThrowable $e) {
    // handle $e->$container data
    throw $e;
} catch (Errors\ForbiddenThrowable $e) {
    // handle $e->$container data
    throw $e;
} catch (Errors\NotFoundThrowable $e) {
    // handle $e->$container data
    throw $e;
} catch (Errors\ConflictThrowable $e) {
    // handle $e->$container data
    throw $e;
} catch (Errors\InviteExpiredThrowable $e) {
    // handle $e->$container data
    throw $e;
} catch (Errors\UnprocessableEntityThrowable $e) {
    // handle $e->$container data
    throw $e;
} catch (Errors\RateLimitExceededThrowable $e) {
    // handle $e->$container data
    throw $e;
} catch (Errors\InternalServerErrorThrowable $e) {
    // handle $e->$container data
    throw $e;
} catch (Errors\SDKException $e) {
    // handle default exception
    throw $e;
}

Server Selection

Server Selection

Select Server by Index

You can override the default server globally by passing a server index to the server_idx: int optional parameter when initializing the SDK client instance. The selected server will then be used as the default on the operations that use it. This table lists the indexes associated with the available servers:

# Server Variables
0 https://api.dub.co None

Override Server URL Per-Client

The default server can also be overridden globally by passing a URL to the server_url: str optional parameter when initializing the SDK client instance. For example:

Development

Maturity

This SDK is in beta, and there may be breaking changes between versions without a major version update. Therefore, we recommend pinning usage to a specific package version. This way, you can install the same version each time without breaking changes unless you are intentionally looking for the latest version.

Contributions

While we value open-source contributions to this SDK, this library is generated programmatically. Feel free to open a PR or a Github issue as a proof of concept and we'll do our best to include it in a future release!

SDK Created by Speakeasy