Installation

composer require codeqr/codeqr-php

Basic Usage

Here’s how you can use the CodeQR PHP SDK to create a link and retrieve click analytics in timeseries format for it:

<?php

declare(strict_types=1);

require 'vendor/autoload.php';

use CodeQR\CodeQR;
use CodeQR\Models\Operations;

// Initialize the CodeQR SDK with your API key
$codeqr = CodeQR::builder()
    ->setSecurity(getenv('CODEQR_API_KEY')) // optional, defaults to CODEQR_API_KEY
    ->build();

// Create a new link
$request = new Operations\CreateLinkRequestBody(
    url: 'https://google.com',
);

try {
    $response = $codeqr->links->create($request);

    if ($response->linkSchema !== null) {
        echo $response->linkSchema->shortLink; // e.g. https://codeqr.link/abc123
    }

    // Get analytics for the link
    $analyticsRequest = new Operations\RetrieveAnalyticsRequest();
    $analyticsRequest->linkId = $response->linkSchema->id;
    $analyticsRequest->interval = Operations\Interval::ThirtyD;
    $analyticsRequest->groupBy = Operations\GroupBy::Timeseries;

    $analyticsResponse = $codeqr->analytics->retrieve($analyticsRequest);

    if ($analyticsResponse->oneOf !== null) {
        print_r($analyticsResponse->oneOf); // e.g. [{ start: "2024-01-01", clicks: 100 }]
    }
} catch (Throwable $e) {
    // handle exception
}

For more usage examples:

  1. Organizing links by external ID, tenant ID, tags, etc
  2. Bulk link operations (create, update, delete)
  3. Retrieving link analytics

Frameworks

You can use the CodeQR PHP SDK with any PHP framework:

  1. Usage with Laravel

If you’re using a different PHP framework, you can refer to the PHP SDK quickstart for a basic example.

Additional Resources