'PHP response XML VAST file
I'm following a low-budget project which cannot afford an expensive VAST Ads Server. Basically, what I have is a group of 5 videos to run as inline preroll. Not much important to track impressions and clicks.
I wrote a few lines of PHP to change randomly the video source and click link into a standard VAST 3.0 XML template.
What I'm missing is how to make the server respond to the video player requests with the XML file.
Here is my code.
Thanks!
$link_1 = 'https://example.com';
$link_2 = 'https://example.com';
$link_3 = 'https://example.com';
$link_4 = 'https://example.com';
$link_5 = 'https://example.com';
$srcvideo_1 = '/path-to-file/1.mp4/';
$srcvideo_2 = '/path-to-file/2.mp4/';
$srcvideo_3 = '/path-to-file/3.mp4/';
$srcvideo_4 = '/path-to-file/4.mp4/';
$srcvideo_5 = '/path-to-file/5.mp4/';
$min=1;
$max=5;
$sessionnum = rand($min,$max);
if ($sessionnum == '1') {
$link = $link_1;
$srcvideo = $srcvideo_1;
}
elseif ($sessionnum == '2') {
$link = $link_2;
$srcvideo = $srcvideo_2;
}
elseif ($sessionnum == '3') {
$link = $link_3;
$srcvideo = $srcvideo_3;
}
elseif ($sessionnum == '4') {
$link = $link_4;
$srcvideo = $srcvideo_4;
}
elseif ($sessionnum == '5') {
$link = $link_5;
$srcvideo = $srcvideo_15;
}
$output = '
<VAST version="3.0">
<Ad id="1">
<InLine>
<AdSystem>Adserver</AdSystem>
<AdTitle>VAST Ad</AdTitle>
<Impression id="12345689">
<![CDATA[ https://example.com/image.png?imp=1 ]]>
</Impression>
<Creatives>
<Creative sequence="1">
<Linear skipoffset="00:00:10">
<Duration>00:00:20</Duration>
<VideoClicks>
<ClickThrough>
<![CDATA[ ' . $link . ' ]]>
</ClickThrough>
<ClickTracking>
<![CDATA[ https://example.com/tracking ]]>
</ClickTracking>
</VideoClicks>
<MediaFiles>
<MediaFile width="426" height="240" delivery="progressive" type="video/mp4" bitrate="261" scalable="true" maintainAspectRatio="true">
<![CDATA[ ' . $srcvideo . ' ]]>
</MediaFile>
</MediaFiles>
</Linear>
</Creative>
</Creatives>
</InLine>
</Ad>
</VAST>
';
?>
Solution 1:[1]
Do not number your variables - use arrays. That is what they are for.
$ads = [
[
'url' => 'https://example.com/',
'video-url' => '/path-to-file/1.mp4/'
],
[
'url' => 'https://example.com/',
'video-url' => '/path-to-file/2.mp4/'
],
[
'url' => 'https://example.com/',
'video-url' => '/path-to-file/3.mp4/'
],
[
'url' => 'https://example.com/',
'video-url' => '/path-to-file/4.mp4/'
],
[
'url' => 'https://example.com/',
'video-url' => '/path-to-file/5.mp4/'
],
];
$ad = $ads[array_rand($ads, 1)];
var_dump($ad);
Example output:
array(2) {
["url"]=>
string(20) "https://example.com/"
["video-url"]=>
string(20) "/path-to-file/2.mp4/"
}
To create XML use a library like DOM. You could use methods to create the whole structure or load a file and manipulate it. In this case I would use a template file because the bootstrap is larger then the actual content.
vast-template.xml:
<VAST version="3.0">
<Ad id="1">
<InLine>
<AdSystem>Adserver</AdSystem>
<AdTitle>VAST Ad</AdTitle>
<Impression id="12345689">https://example.com/image.png?imp=1</Impression>
<Creatives>
<Creative sequence="1">
<Linear skipoffset="00:00:10">
<Duration>00:00:20</Duration>
<VideoClicks>
<ClickThrough/>
<ClickTracking>https://example.com/tracking</ClickTracking>
</VideoClicks>
<MediaFiles>
<MediaFile
width="426" height="240"
delivery="progressive"
type="video/mp4" bitrate="261"
scalable="true"
maintainAspectRatio="true"/>
</MediaFiles>
</Linear>
</Creative>
</Creatives>
</InLine>
</Ad>
</VAST>
Note the the ClickThrough and MediaFile elements are empty. Next load the template and fill the elements.
$document = new DOMDocument();
$document->load(__DIR__.'/vast-template.xml');
$xpath = new DOMXpath($document);
$link = $xpath->evaluate('//ClickThrough')->item(0);
$link->textContent = $ad['url'];
$file = $xpath->evaluate('//MediaFile')->item(0);
$file->textContent = $ad['video-url'];
echo $document->saveXML();
Example output:
<?xml version="1.0"?>
<VAST version="3.0">
<Ad id="1">
<InLine>
<AdSystem>Adserver</AdSystem>
<AdTitle>VAST Ad</AdTitle>
<Impression id="12345689">https://example.com/image.png?imp=1</Impression>
<Creatives>
<Creative sequence="1">
<Linear skipoffset="00:00:10">
<Duration>00:00:20</Duration>
<VideoClicks>
<ClickThrough>https://example.com/</ClickThrough>
<ClickTracking>https://example.com/tracking</ClickTracking>
</VideoClicks>
<MediaFiles>
<MediaFile width="426" height="240" delivery="progressive" type="video/mp4" bitrate="261" scalable="true" maintainAspectRatio="true">/path-to-file/2.mp4/</MediaFile>
</MediaFiles>
</Linear>
</Creative>
</Creatives>
</InLine>
</Ad>
</VAST>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ThW |
