1. Prerequisites

To follow this guide, you will need to:

2. Install and initialize the CodeQR PHP SDK

1

Install

To install the CodeQR PHP SDK, run the following command:

composer require codeqr/codeqr-php
2

Initialize

Initialize the CodeQR PHP SDK by creating a new instance of the CodeQR class.

index.php
declare(strict_types=1);

require 'vendor/autoload.php';

use CodeQR\CodeQR;
use CodeQR\Models\Components;

$codeqr = CodeQR::builder()->setSecurity("CODEQR_API_KEY")->build();

Let’s create a short link using the CodeQR PHP SDK.

index.php
use CodeQR\Models\Operations;

try {
  $request = new Operations\CreateLinkRequestBody(
    url: 'https://google.com',
  );

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

  if ($response->linkSchema !== null) {
    // handle response
  }
} catch (Throwable $e) {
  // handle exception
}

Optionally, you can also pass an externalId field which is a unique identifier for the link in your own database to associate it with the link in CodeQR’s system.

index.php
use CodeQR\Models\Operations;

try {
  $request = new Operations\CreateLinkRequestBody(
    url: 'https://google.com',
    externalId: '12345',
  );

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

  if ($response->linkSchema !== null) {
    // handle response
  }
} catch (Throwable $e) {
  // handle exception
}

This will let you easily update the link or retrieve analytics for it later on using the externalId instead of the CodeQR linkId.

CodeQR PHP SDK provides a method to upsert a link – where an existing link is updated if it exists, or a new link is created if it doesn’t. so you don’t have to worry about checking if the link already exists.

index.php
use CodeQR\Models\Operations;

try {
  $request = new Operations\UpsertLinkRequestBody(
    url: 'https://google.com',
  );

  $response = $codeqr->links->upsert($request);

  if ($response->linkSchema !== null) {
    // handle response
  }
} catch (Throwable $e) {
  // handle exception
}

This way, you won’t have to worry about checking if the link already exists when you’re creating it.

Let’s update an existing link using the CodeQR PHP SDK.

You can do that in two ways:

  • Using the link’s linkId in CodeQR’s system.
  • Using the link’s externalId in your own database (prefixed with ext_).
index.php
use CodeQR\Models\Operations;

try {
    $request = new Operations\UpdateLinkRequest();
    $request->linkId = 'cly2p8onm000cym8200nfay7l';
    $request->requestBody = new Operations\UpdateLinkRequestBody();
    $request->requestBody->url = 'https://google.us';

    $response = $codeqr->links->update($request);

    if ($response->linkSchema !== null) {
        echo $response->linkSchema->shortLink;
    }
} catch (Throwable $e) {
    // handle exception
}

Let’s retrieve analytics for a link using the CodeQR PHP SDK.

index.php
use CodeQR\Models\Operations;

try {
    $request = new Operations\RetrieveAnalyticsRequest();
    $request->linkId = 'clmnr6jcc0005l308q9v56uz1';
    $request->interval = Operations\Interval::SevenD;
    $request->groupBy = Operations\GroupBy::Timeseries;

    $response = $codeqr->analytics->retrieve($request);

    if ($response->oneOf !== null) {
        // Handle the response
        print_r($response->oneOf);
    }
} catch (Throwable $e) {
    // handle exception
}

7. Examples