PHP code example of cutmeshort / sdk

1. Go to this page and download the library: Download cutmeshort/sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cutmeshort / sdk example snippets



CutMeShort\SDK\Client;
use CutMeShort\SDK\Models\LeadPayload;

// Initialize the SDK
$client = new Client(
    accessToken: 'your-api-token',
    host: 'https://www.cutmeshort.com'
);

// Track a lead
$leadPayload = new LeadPayload();
$leadPayload->setClickId('click_123456')
    ->setEventName('form_submission')
    ->setCustomerExternalId('customer_789')
    ->setCustomerName('John Doe')
    ->setCustomerEmail('[email protected]')
    ->setTimestamp(new DateTime());

$response = $client->getTrackingApi()->trackLead($leadPayload);

echo "Lead tracked: " . ($response->getSuccess() ? 'Success' : 'Failed') . "\n";

use CutMeShort\SDK\Models\SalePayload;

$salePayload = new SalePayload();
$salePayload->setClickId('click_123456')
    ->setEventName('purchase')
    ->setCustomerExternalId('customer_789')
    ->setInvoiceId('inv_001')
    ->setAmount(99.99)
    ->setCurrency('USD')
    ->setTimestamp(new DateTime());

$response = $client->getTrackingApi()->trackSale($salePayload);

$client = new Client();
$client->setAccessToken('your-bearer-token')
    ->setHost('https://api.cutmeshort.com');

$config = $client->getConfig();

// Set custom headers
$config->setUserAgent('MyApp/1.0.0');

// API key authentication (if needed)
$config->setApiKey('X-API-Key', 'your-api-key');

// Enable debug mode
$client->setDebug(true);

// Custom certificate for mTLS
$config->setCertFile('/path/to/cert.pem');
$config->setKeyFile('/path/to/key.pem');

$payload = new LeadPayload();
$payload->setClickId('click_id')              // Required: Click identifier
    ->setEventName('signup')                   // Required: Event name
    ->setCustomerExternalId('customer_id')     // Customer ID from your system
    ->setCustomerName('John Doe')              // Customer name
    ->setCustomerEmail('[email protected]')     // Customer email
    ->setCustomerAvatar('https://...')         // Avatar URL
    ->setTimestamp(new DateTime())             // Event timestamp
    ->setMode('deferred');                     // Optional: 'deferred' for first attribution

$payload = new SalePayload();
$payload->setClickId('click_id')              // Required
    ->setEventName('purchase')                 // Required
    ->setCustomerExternalId('customer_id')
    ->setCustomerName('John Doe')
    ->setCustomerEmail('[email protected]')
    ->setCustomerAvatar('https://...')
    ->setInvoiceId('inv_001')                  // Invoice identifier
    ->setAmount(99.99)                         // Sale amount
    ->setCurrency('USD')                       // Currency code
    ->setTimestamp(new DateTime());

use CutMeShort\SDK\Exceptions\SdkException;

try {
    $response = $client->getTrackingApi()->trackLead($payload);
} catch (SdkException $e) {
    echo "API Error: " . $e->getMessage() . "\n";
    echo "Status Code: " . $e->getCode() . "\n";
    
    if ($e->getResponseObject()) {
        // Access error response object
        $errorResponse = $e->getResponseObject();
    }
} catch (Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
}

// Step 1: Track initial lead with deferred mode
$leadPayload = new LeadPayload();
$leadPayload->setClickId('click_deferred_123')
    ->setCustomerExternalId('customer_789')
    ->setEventName('initial_lead')
    ->setMode('deferred');

$client->getTrackingApi()->trackLead($leadPayload);

// Step 2: Send subsequent lead events (backend will resolve association)
$followupPayload = new LeadPayload();
$followupPayload->setClickId('click_deferred_123')
    ->setCustomerExternalId('customer_789')
    ->setEventName('conversion');

$client->getTrackingApi()->trackLead($followupPayload);