ajaxloading openx with jquery and php

岁酱吖の 提交于 2019-12-10 11:47:51

问题


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>';
}

回答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");


来源:https://stackoverflow.com/questions/18955887/ajaxloading-openx-with-jquery-and-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!