PHP code example of villaflor / taboola-sdk

1. Go to this page and download the library: Download villaflor/taboola-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/ */

    

villaflor / taboola-sdk example snippets


use Villaflor\Connection\Auth\APIToken;
use Villaflor\Connection\Auth\None;
use Villaflor\TaboolaSDK\Configurations\AuthenticationConfiguration;
use Villaflor\TaboolaSDK\Endpoints\Authentication;
use Villaflor\TaboolaSDK\TaboolaClient;

$clientId = 'your-client-id';
$clientSecret = 'your-client-secret';
$accountId = 'your-account-id';

// Get access token
$authEndpoint = new Authentication(new TaboolaClient(new None()));
$response = $authEndpoint->getAccessToken(
    new AuthenticationConfiguration($clientId, $clientSecret)
);

// Access token is now type-safe with AuthenticationResponse DTO
$accessToken = $response->accessToken;
$expiresIn = $response->expiresIn;
$tokenType = $response->tokenType;

use Villaflor\TaboolaSDK\Configurations\Account\AccountDetailsConfiguration;
use Villaflor\TaboolaSDK\Configurations\Account\AdvertiserAccountsInNetworkConfiguration;
use Villaflor\TaboolaSDK\Configurations\Account\AllowedAccountsConfiguration;
use Villaflor\TaboolaSDK\Endpoints\Account;

// Create authenticated client
$client = new TaboolaClient(new APIToken($accessToken));
$account = new Account($client);

// Get account details - Returns AccountResponse DTO
$details = $account->getAccountDetails(new AccountDetailsConfiguration());
$accountData = $details->body;

// Get advertiser accounts in network - Returns CollectionResponse DTO
$advertisers = $account->getAdvertiserAccountsInNetwork(
    new AdvertiserAccountsInNetworkConfiguration($accountId)
);
$results = $advertisers->results;
$metadata = $advertisers->metadata;

// Get allowed accounts - Returns CollectionResponse DTO
$allowed = $account->getAllowedAccounts(new AllowedAccountsConfiguration());

use Villaflor\TaboolaSDK\Configurations\Campaigns\AllCampaignsConfiguration;
use Villaflor\TaboolaSDK\Definitions\AllCampaignsFilterDefinition;
use Villaflor\TaboolaSDK\Endpoints\Campaigns;

$campaigns = new Campaigns($client);

// Get all campaigns - Returns CollectionResponse DTO
$response = $campaigns->getAllCampaigns(
    new AllCampaignsConfiguration(
        accountId: $accountId,
        filters: [
            AllCampaignsFilterDefinition::FETCH_LEVEL =>
                AllCampaignsFilterDefinition::FETCH_LEVEL_RECENT_AND_PAUSED_OPTIONS
        ]
    )
);

$campaignList = $response->results;
$metadata = $response->metadata;

use Villaflor\TaboolaSDK\Configurations\Reporting\CampaignSummaryConfiguration;
use Villaflor\TaboolaSDK\Configurations\Reporting\TopCampaignContentConfiguration;
use Villaflor\TaboolaSDK\Definitions\CampaignSummaryDimensionDefinition;
use Villaflor\TaboolaSDK\Definitions\CampaignSummaryFilterDefinition;
use Villaflor\TaboolaSDK\Definitions\TopCampaignContentDimensionDefinition;
use Villaflor\TaboolaSDK\Definitions\TopCampaignContentFilterDefinition;
use Villaflor\TaboolaSDK\Endpoints\Reporting;

$reporting = new Reporting($client);

// Campaign summary report - Returns ReportResponse DTO
$summary = $reporting->getCampaignSummaryReport(
    new CampaignSummaryConfiguration(
        accountId: $accountId,
        dimension: CampaignSummaryDimensionDefinition::CAMPAIGN_DAY_BREAKDOWN,
        filters: [
            CampaignSummaryFilterDefinition::START_DATE => '2021-08-01',
            CampaignSummaryFilterDefinition::END_DATE => '2021-08-31',
        ]
    )
);

// Access type-safe response properties
$timezone = $summary->timezone;
$results = $summary->results;
$recordCount = $summary->recordCount;
$metadata = $summary->metadata;

// Top campaign content report - Returns ReportResponse DTO
$topContent = $reporting->getTopCampaignContentReport(
    new TopCampaignContentConfiguration(
        accountId: $accountId,
        dimension: TopCampaignContentDimensionDefinition::ITEM_BREAKDOWN,
        filters: [
            TopCampaignContentFilterDefinition::START_DATE => '2021-08-01',
            TopCampaignContentFilterDefinition::END_DATE => '2021-08-31',
        ]
    )
);

use Villaflor\TaboolaSDK\Exceptions\JsonDecodeException;
use Villaflor\TaboolaSDK\Exceptions\TaboolaException;

try {
    $response = $authEndpoint->getAccessToken($config);
} catch (JsonDecodeException $e) {
    // Handle JSON decoding errors
    echo "Failed to decode API response: " . $e->getMessage();
} catch (TaboolaException $e) {
    // Handle other Taboola SDK errors
    echo "SDK Error: " . $e->getMessage();
}

use function Tests\loadFixture;
use function Tests\mockResponseFromFixture;
use function Tests\mockResponse;

// Load fixture data as array
$data = loadFixture('Campaigns/single_campaign.json');

// Create mock response from fixture
$response = mockResponseFromFixture('Campaigns/single_campaign.json');

// Create mock response from array
$response = mockResponse(['status' => 'success']);
bash
composer analyse