Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/sokil/php-vast

:star: Generating and parsing VAST documents
https://github.com/sokil/php-vast

ads advert advertising banner iab media php vast video xml

Last synced: 7 days ago
JSON representation

:star: Generating and parsing VAST documents

Awesome Lists containing this project

README

        

# Stand With Ukraine

[![SWUbanner](https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/main/banner-direct.svg)](https://github.com/vshymanskyy/StandWithUkraine/blob/main/docs/README.md)

----

PHP-VAST
========

[![Build](https://github.com/sokil/php-vast/workflows/Test/badge.svg?branch=2.0)](https://github.com/sokil/php-vast/actions?query=workflow%3ATest)
[![Total Downloads](http://img.shields.io/packagist/dt/sokil/php-vast.svg?1)](https://packagist.org/packages/sokil/php-vast)
[![Coverage Status](https://coveralls.io/repos/github/sokil/php-vast/badge.svg?branch=master&1)](https://coveralls.io/github/sokil/php-vast?branch=master)

:star: VAST Ad generator and parser library on PHP.

## Specs
* VAST 2.0 Spec: http://www.iab.net/media/file/VAST-2_0-FINAL.pdf
* VAST 3.0 Spec: http://www.iab.com/wp-content/uploads/2015/06/VASTv3_0.pdf
* VAST 4.0 Spec:
* http://www.iab.com/wp-content/uploads/2016/01/VAST_4-0_2016-01-21.pdf
* https://www.iab.com/wp-content/uploads/2016/04/VAST4.0_Updated_April_2016.pdf
* VAST 4.1 Spec:
* https://iabtechlab.com/wp-content/uploads/2018/11/VAST4.1-final-Nov-8-2018.pdf
* [VAST Samples](https://github.com/InteractiveAdvertisingBureau/VAST_Samples)

## Install

Install library through composer:

```
composer require sokil/php-vast
```

## Quick start

```php
// create document
$factory = new \Sokil\Vast\Factory();
$document = $factory->create('4.1');

// insert Ad section
$ad1 = $document
->createInLineAdSection()
->setId('ad1')
->setAdSystem('Ad Server Name')
->setAdTitle('Ad Title')
->addImpression('http://ad.server.com/impression', 'imp1');

// create creative for ad section
$linearCreative = $ad1
->createLinearCreative()
->setDuration(128)
->setId('013d876d-14fc-49a2-aefd-744fce68365b')
->setAdId('pre')
->setVideoClicksClickThrough('http://entertainmentserver.com/landing')
->addVideoClicksClickTracking('http://ad.server.com/videoclicks/clicktracking')
->addVideoClicksCustomClick('http://ad.server.com/videoclicks/customclick')
->addTrackingEvent('start', 'http://ad.server.com/trackingevent/start')
->addTrackingEvent('pause', 'http://ad.server.com/trackingevent/stop');

// add closed caption file (Closed Caption support starts on VAST 4.1)
$linearCreative
->createClosedCaptionFile()
->setLanguage('en-US')
->setType('text/srt')
->setUrl('http://server.com/cc.srt');

// add 100x100 media file
$linearCreative
->createMediaFile()
->setProgressiveDelivery()
->setType('video/mp4')
->setHeight(100)
->setWidth(100)
->setBitrate(2500)
->setUrl('http://server.com/media1.mp4');

// add 200x200 media file
$linearCreative
->createMediaFile()
->setProgressiveDelivery()
->setType('video/mp4')
->setHeight(200)
->setWidth(200)
->setBitrate(2500)
->setUrl('http://server.com/media2.mp4');

// get dom document
$domDocument = $document->toDomDocument();

// get XML string
echo $document;
```

This will generate:

```xml



Ad Server Name





00:02:08


























```

## Custom Specification Support

VAST document elements are completely described in it's specification, but some Ad servers may add support for custom elements and attributes. This library strictly follows specification, generally because two dialects of VAST may conflict with each other. You may write our own dialect by overriding element builder and create any elements and attributes you want.

The VAST dialect is described in `\Sokil\Vast\ElementBuilder` class. By overriding it you may create instances of your own classes and add there any setters.

First let's create a class for `MediaFile` and add some custom attributes:

```php
domElement->setAttribute('minDuration', $seconds);

return $this;
}
}
```

Now we need to override the default element builder and create our own `MediaFile` factory method:

```php

*
* @param \DOMElement $mediaFileDomElement
*
* @return AcmeMediaFile
*/
public function createInLineAdLinearCreativeMediaFile(\DOMElement $mediaFileDomElement)
{
return new AcmeMediaFile($mediaFileDomElement);
}
}
```

Now we need to confugure VAST factory to use overridden element builder:

```php
create('4.1');

$ad = $document->createInLineAdSection();
$creative = $ad->createLinearCreative();
$mediaFile = $creative->createMediaFile();

$mediaFile->setMinDiration(10);
```

If you have an AD server and want to add support for your custom tag, create your own library with custom elements and element builder, or add a pull request to this library.