PHP code example of audiencerun / iabtcf

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

    

audiencerun / iabtcf example snippets


$cs = IABTcf\Decoder::decode("COtybn4PA_zT4KjACBENAPCIAD-AAECAAIAAAxAAAAgAIAwgAgAAAAEAgQAAAAAEAYQAQAAAACAAAABACBQAYAAgAEEgBAABAAQA.IBAgAAAgAIAwgAgAAAAEAAAACA.Qu4QBQAGAAXABLAC8AMAu4A.cAAAAAAAITg");

// Core String
var_dump($cs->getVersion());

int(2)

var_dump($cs->getCreated()->format("c"));

string(25) "2020-01-26T17:01:00+00:00"

var_dump($cs->getLastUpdated()->format("c"));

string(25) "2021-02-02T17:01:00+00:00"

var_dump($cs->getCmpId());

int(675)

var_dump($cs->getCmpVersion());

int(2)

var_dump($cs->getConsentScreen());

int(1)

var_dump($cs->getVendorListVersion());

int(15)

var_dump($cs->getTcfPolicyVersion());

int(2)

var_dump($cs->getConsentLanguage());

string(2) "en"

var_dump($cs->getPublisherCC());

string(2) "aa"

var_dump($cs->getIsServiceSpecific());

bool(false)

var_dump($cs->getPurposeOneTreatment());

bool(true)

var_dump($cs->getUseNonStandardStacks());

bool(false)

var_dump($cs->getPurposesConsent());

array(7) {
  [0]=>
  int(3)
  [1]=>
  int(4)
  [2]=>
  int(5)
  [3]=>
  int(6)
  [4]=>
  int(7)
  [5]=>
  int(8)
  [6]=>
  int(9)
}

var_dump($cs->getVendorConsent());

array(7) {
  [0]=>
  int(23)
  [1]=>
  int(37)
  [2]=>
  int(47)
  [3]=>
  int(48)
  [4]=>
  int(53)
  [5]=>
  int(65)
  [6]=>
  int(98)
}

var_dump($cs->getVendorLegitimateInterest());

array(7) {
  [0]=>
  int(37)
  [1]=>
  int(47)
  [2]=>
  int(48)
  [3]=>
  int(53)
  [4]=>
  int(65)
  [5]=>
  int(98)
  [6]=>
  int(129)
}

var_dump($cs->getSpecialFeatureOptIns());

array(1) {
  [0]=>
  int(1)
}

var_dump($cs->getPurposesLITransparency());

array(2) {
  [0]=>
  int(2)
  [1]=>
  int(9)
}

$pubRestrictions = $cs->getPublisherRestrictions();
var_dump($pubRestrictions[0]->getPurposeId());

int(1)

var_dump($pubRestrictions[0]->getRestrictionType()->getId());

int(1)

var_dump($pubRestrictions[0]->getVendorIds());

array(2) {
  [0]=>
  int(1)
  [1]=>
  int(2)
}

// Segment 1: Disclosed vendors
var_dump($cs->getDisclosedVendors());

array(8) {
  [0]=>
  int(23)
  [1]=>
  int(37)
  [2]=>
  int(47)
  [3]=>
  int(48)
  [4]=>
  int(53)
  [5]=>
  int(65)
  [6]=>
  int(98)
  [7]=>
  int(129)
}

// Segment 2: Allowed vendors
var_dump($cs->getAllowedVendors());

array(6) {
  [0]=>
  int(12)
  [1]=>
  int(23)
  [2]=>
  int(37)
  [3]=>
  int(47)
  [4]=>
  int(48)
  [5]=>
  int(6000)
}

// Segment 3: Publisher purposes
var_dump($cs->getPubPurposesConsent());

array(1) {
  [0]=>
  int(1)
}

var_dump($cs->getPubPurposesLITransparency());

array(1) {
  [0]=>
  int(24)
}

var_dump($cs->getCustomPurposesConsent());

array(1) {
  [0]=>
  int(2)
}

var_dump($cs->getCustomPurposesLITransparency());

array(2) {
  [0]=>
  int(1)
  [1]=>
  int(2)
}

$cs = new IABTcf\ConsentString();
$cs->setVersion(2);
$cs->setCreated(new DateTime('2020-01-26 17:01:00'));
$cs->setLastUpdated(new DateTime('2021-02-02 17:01:00'));
$cs->setCmpId(675);
$cs->setCmpVersion(2);
$cs->setConsentScreen(1);
$cs->setVendorListVersion(15);
$cs->setTcfPolicyVersion(2);
$cs->setConsentLanguage('en');
$cs->setPublisherCC('aa');
$cs->setIsServiceSpecific(false);
$cs->setPurposeOneTreatment(true);
$cs->setUseNonStandardStacks(false);
$cs->setAllowedPurposes([2, 10]);
$cs->setSpecialFeatureOptIns([1]);
$cs->setPurposesConsent([3, 4, 5, 6, 7, 8, 9]);
$cs->setVendorConsent([23, 37, 47, 48, 53, 65, 98]);
$cs->setVendorLegitimateInterest([37, 47, 48, 53, 65, 98, 129]);
$cs->setPurposesLITransparency([2, 9]);
$restrictionType = new IABTcf\TCFv2\RestrictionType(1);
$cs->setPublisherRestrictions([
    new IABTcf\TCFv2\PublisherRestriction(1, $restrictionType, [1, 2]),
    new IABTcf\TCFv2\PublisherRestriction(2, $restrictionType, [1, 8])
]);

$cs->setDisclosedVendors([23, 37, 47, 48, 53, 65, 98, 129]);

$cs->setAllowedVendors([12, 23, 37, 47, 48, 6000]);

$cs->setPubPurposesConsent([1]);
$cs->setPubPurposesLITransparency([24]);
$cs->setCustomPurposesConsent([2]);
$cs->setCustomPurposesLITransparency([1, 2]);

// Encode the data into a web-safe base64 string
$enc = IABTcf\Encoder::encode($cs);


echo $enc;

Outputs:

COtybn4PA_zT4KjACBENAPCIAD-AAECAAIAAAxAAAAgAIAwgAgAAAAEAgQAAAAAEAYQAQAAAACAAAABACBQAYAAgAEEgBAABAAQA.IBAgAAAgAIAwgAgAAAAEAAAACA.Qu4QBQAGAAXABLAC8AMAu4A.cAAAAAAAITg