ajaxloading openx with jquery and php
Asked Answered
G

1

0

after a long journey I managed to ajax-load my openx-ads with a combination of jquery an php.

You need is

  1. an openx-server on your own and access to /{openxPath}/www/delivery/alocal.php.
  2. a little wrapper that makes the ad-script ajaxable
  3. an ajax-loader

The third and easiest part is the ajax-loader:

$(document).ready( function() {
    $.ajax({
        url: "http://{urlToYourOpenxWrapper/adwrapper.php",
        type: "POST",
        data: {m:'f'},  // 'code' of ad to load
        async: false,
        dataType: 'html'
    }).done(function (answer) {
        $('#footerBanner').html(answer);
    });
});

The second part is a little bit tricky an maybe not future-proof. But for 2.8.11 it is working. For security reasons I made a mapping from characters to zone-ids. I don't know if this is really necessary.

adwrapper.php:

define('MAX_PATH', 'pathToYoutOpenXServer');
if (@include_once(MAX_PATH . '/www/delivery/alocal.php')) {
    if (!isset($phpAds_context)) {
        $phpAds_context = array();
    }
    switch ($_POST["m"]) {
        case 'f':  // code of the ad to load
            $zoneId = 12;
            $bannerTarget = 'footerBanner zone_' . $zoneId;
            $bannerCode = view_local('', 12, 0, 0, '', '', '0', $phpAds_context, '');
            break;
    }
      // get banner id
    $regex = '/(.*)(ox_[^\']*)(.*)/';
    preg_match($regex, $bannerCode['html'], $matches);
    $oxId = $matches[2];
      // compile new insert code
    $replaceWith = '$("' . $oxId . '").after';
    $banner = str_replace('document.write', $replaceWith, $bannerCode['html']);
    $banner = str_replace('<script type=\'text/javascript\' src=\'http://openx.lift-online.de/www/delivery/fl.js\'></script>' ,
        '<!-- replaced -->' ,
        $banner);

      // use a single object for each ad to prevent problem in multitasking
    $banner = str_replace('ox_swf', 'ox_swf_' . $zoneId, $banner);

      // sometime the oxId (unique Id???) is the same and than zones are mixed
      // so I append the zoneId to the oxId
    $banner = str_replace($oxId, $oxId . '_' . $zoneId, $banner);
    echo '<div class="' . $bannerTarget . '">' . $banner . '</div>';
}
Glutamine answered 23/9, 2013 at 9:29 Comment(1)
This question appears to be off-topic because there is no questionMislay
S
1

Thanks, working on it and fail fail fail, but great solution from you !

I just need using GET request and must add on the php wrapper

header("Access-Control-Allow-Origin: *");
header("Access-Control-Request-Method: GET");
Sachet answered 19/10, 2013 at 16:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.