PHPackages                             sokil/php-vast - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Image &amp; Media](/categories/media)
4. /
5. sokil/php-vast

ActiveLibrary[Image &amp; Media](/categories/media)

sokil/php-vast
==============

Generator and parser for VAST documents

2.2.1(3y ago)82157.4k—5.5%37[2 issues](https://github.com/sokil/php-vast/issues)MITPHPPHP &gt;=7.1.3

Since Jan 25Pushed 2y ago8 watchersCompare

[ Source](https://github.com/sokil/php-vast)[ Packagist](https://packagist.org/packages/sokil/php-vast)[ RSS](/packages/sokil-php-vast/feed)WikiDiscussions 2.0 Synced 1mo ago

READMEChangelog (10)Dependencies (3)Versions (27)Used By (0)

Stand With Ukraine
==================

[](#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
========

[](#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](https://camo.githubusercontent.com/fedc3f7728b61c26909ece99e566e0a9b2505d94029d340d772715b1624af68c/687474703a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f736f6b696c2f7068702d766173742e7376673f31)](https://packagist.org/packages/sokil/php-vast)[![Coverage Status](https://camo.githubusercontent.com/e3cc8f8109c0acf2efb6113d5a44e50f814c136361747dac98a3ac70add7c26f/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f736f6b696c2f7068702d766173742f62616467652e7376673f6272616e63683d6d61737465722631)](https://coveralls.io/github/sokil/php-vast?branch=master)

⭐ VAST Ad generator and parser library on PHP.

Specs
-----

[](#specs)

- VAST 2.0 Spec: [http://www.iab.net/media/file/VAST-2\_0-FINAL.pdf](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](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](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](https://www.iab.com/wp-content/uploads/2016/04/VAST4.0_Updated_April_2016.pdf)
- VAST 4.1 Spec:
    -
- [VAST Samples](https://github.com/InteractiveAdvertisingBureau/VAST_Samples)

Install
-------

[](#install)

Install library through composer:

```
composer require sokil/php-vast

```

Quick start
-----------

[](#quick-start)

```
// 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:

```

            Ad Server Name
            Ad Title]]>
            http://ad.server.com/impression]]>

                        00:02:08

                            http://entertainmentserver.com/landing]]>
                            http://ad.server.com/videoclicks/clicktracking]]>
                            http://ad.server.com/videoclicks/customclick]]>

                            http://ad.server.com/trackingevent/start]]>
                            http://ad.server.com/trackingevent/stop]]>

                                  http://server.com/cc.srt]]>

                                http://server.com/media1.mp4]]>

                                http://server.com/media2.mp4]]>

```

Custom Specification Support
----------------------------

[](#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:

```
