PHP code example of be-lenka / tiktok-php-sdk

1. Go to this page and download the library: Download be-lenka/tiktok-php-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/ */

    

be-lenka / tiktok-php-sdk example snippets


$tiktok = new \Belenka\TikTok\TikTok($token, $pixelId);

// Set user info
$user = new \Belenka\TikTok\Models\User();
$user->setUserAgent($_SERVER['HTTP_USER_AGENT'])
     ->setIpAddress($_SERVER['REMOTE_ADDR'])
     ->setEmails(['[email protected]']) // optional
     ->setPhones(['+421901123456']) // optional
     ->setClickId($_REQUEST['ttclid']) // if available
     ->setCookieId($_COOKIE['ttp'])
     ->setExternalIds(['user-id-in-your-system']) // optional
;

// Set page info
$page = new \Belenka\TikTok\Models\Page();
$page->setUrl('https://example.com')
     ->setReferrer('https://example.com') // optional
;

// Set products
$contents = [];
$orderItems = []; // your order items list
foreach($orderItems as $item) {
     $contents[] = (new \Belenka\TikTok\Models\Content)
          ->setPrice($item->price)
          ->setQuantity($item->quantity)
          ->setContentId($item->product_id)
          ->setContentName($item->item_title)
     ;
}

$properties = new \Belenka\TikTok\Models\Property();
$properties->setCurrency('USD')
     ->setQuery('COUPON_CODE')
     ->setValue(100.99)
     ->setOrderId('order_id')
     ->setContents($contents)
;

// Set CompletePayment event
$eventA = new \Belenka\TikTok\Models\Event();
$eventA->setEventName(\Belenka\TikTok\Enums\EventName::COMPLETE_PAYMENT)
     ->setEventTime(time())
     ->setEventId($order->uuid)
     ->setUser($user)
     ->setPage($page)
     ->setProperties($properties)
;

// Set PlaceAnOrder event
$eventB = new \Belenka\TikTok\Models\Event();
$eventB->setEventName(\Belenka\TikTok\Enums\EventName::PLACE_AN_ORDER)
     ->setEventTime(time())
     ->setEventId($order->uuid)
     ->setUser($user)
     ->setPage($page)
     ->setProperties($properties)
;

$eventRequest = $tiktok->events()
     ->setEventSource(\Belenka\TikTok\Enums\EventSource::WEB)
     ->setTestEventCode($testCode) // optional
;

try {
     $eventRequest->execute($eventA);
     if ($eventRequest->isSuccessful()) {
          echo "Result: Success";
     }

     echo "Response:";
     $responseBody = $eventRequest->getResponseBody();
     // ...
} catch (\Exception $e) {
    echo "Error: " . $e->getMessage();
}

$eventRequest->execute([$eventA, $eventB]);