ajaxloading openx with jquery and php


ajaxloading openx with jquery and php

经过漫长的旅程,我设法用jquery和php的组合ajax加载了我的openx-ads。

你需要的是

  1. 您自己的 openx-server 并访问/{openxPath}/www/delivery/alocal.php。
  2. 一个小包装器,使广告脚本变得可雅
  3. 一个 ajax-loader

第三个也是最简单的部分是 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);
    });
});

第二部分有点棘手,也许不是面向未来的。但是对于 2.8.11,它正在工作。出于安全原因,我做了从字符到区域ID的映射。我不知道这是否真的有必要。

广告包装器.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>';
}

谢谢,努力工作并失败失败失败,但您的解决方案很棒!

我只需要使用 GET 请求,并且必须添加 php 包装器

header("Access-Control-Allow-Origin: *");
header("Access-Control-Request-Method: GET");