本文介紹一個功能完善的 PHP 廣告加載類 ADLoader,支援異步與同步兩種加載模式,並能根據瀏覽器特性自動切換策略,依賴 jQuery 庫實現前端渲染。
<?php
/**
* 廣告加載管理類
* Date: 2013-08-04
* Author: fdipzone
* Ver: 1.0
*/
class ADLoader {
private static $_ads = array(); // 廣告集合
private static $_step = 300; // 廣告加載間隔(毫秒)
private static $_async = true; // 是否異步加載
private static $_config = array(); // 廣告配置
private static $_jsclass = null; // 廣告 JS 類文件路徑
/**
* 加載廣告集合
* @param string $channel 頻道名,對應配置文件
* @param int $step 加載間隔
* @param bool $async 是否異步加載
* @return string 生成的 HTML/JS 代碼
*/
public static function load($channel = '', $step = '', $async = '') {
if (isset($step) && is_numeric($step) && $step > 0) {
self::$_step = $step;
}
if (isset($async) && is_bool($async)) {
self::$_async = $async;
}
// 瀏覽器檢測,IE 強制同步
if (!self::checkBrowser()) {
self::$_async = false;
}
self::getAds($channel);
self::genZoneId();
return self::genHtml();
}
/** 設置配置 */
public static function setConfig($config = array(), $jsclass = '') {
self::$_config = $config;
self::$_jsclass = $jsclass;
}
/** 根據頻道獲取廣告集合 */
private static function getAds($channel = '') {
$AD_Config = self::$_config;
if ($AD_Config != null) {
self::$_ads = isset($AD_Config[$channel]) ? $AD_Config[$channel] : $AD_Config['default'];
}
}
/** Zone ID Base64 編碼處理 */
private static function genZoneId() {
if (!self::$_async) return; // 同步模式無需處理
$ads = self::$_ads;
for ($i = 0, $len = count($ads); $i < $len; $i++) {
if (isset($ads[$i]['zoneId'])) {
$ads[$i]['zoneId'] = base64_encode('var zoneid=' . $ads[$i]['zoneId'] . ';');
}
}
self::$_ads = $ads;
}
/** 生成廣告 HTML/JS 代碼 */
private static function genHtml() {
$ads = self::$_ads;
$html = array();
if (self::$_jsclass != null && $ads) {
// 引入廣告 JS 類
array_push($html, '<script type="text/javascript" src="' . self::$_jsclass . '"></script>');
// 同步模式預加載
if (!self::$_async) {
foreach ($ads as $ad) {
array_push($html, '<div id="' . $ad['domId'] . '_container" style="display:none">');
array_push($html, '<script type="text/javascript">');
array_push($html, 'ADLoader.preload(' . json_encode($ad) . ');');
array_push($html, '</script>');
array_push($html, '</div>');
}
}
}
array_push($html, '<script type="text/javascript">');
array_push($html, 'var ads=' . json_encode($ads) . ';');
array_push($html, '$(document).ready(function(){ ADLoader.load(ads, ' . self::$_step . ', ' . intval(self::$_async) . '); });');
array_push($html, '</script>');
return implode("\r\n", $html);
}
/** 檢測是否需強制同步加載的瀏覽器 */
private static function checkBrowser() {
$user_agent = $_SERVER['HTTP_USER_AGENT'];
if (strstr($user_agent, 'MSIE') !== false) {
return false;
}
return true;
}
}
?>
<?php
/** 廣告配置文件 **/
return array(
'case_openx' => array(
array('type' => 'openx', 'domId' => 'ad_728x90', 'zoneId' => 452),
array('type' => 'openx', 'domId' => 'ad_300x250', 'zoneId' => 449),
array('type' => 'openx', 'domId' => 'ad_l2_300x250', 'zoneId' => 394),
),
'case_url' => array(
array('type' => 'url', 'domId' => 'ad_728x90', 'url' => 'adurl.php?zoneid=452'),
array('type' => 'url', 'domId' => 'ad_300x250', 'url' => 'adurl.php?zoneid=449'),
array('type' => 'url', 'domId' => 'ad_l2_300x250', 'url' => 'adurl.php?zoneid=394'),
),
'case_sync_openx' => array(
array('type' => 'openx', 'domId' => 'ad_728x90', 'zoneId' => 452),
array('type' => 'openx', 'domId' => 'ad_300x250', 'zoneId' => 449),
array('type' => 'openx', 'domId' => 'ad_l2_300x250', 'zoneId' => 394),
),
'default' => array(
array('type' => 'openx', 'domId' => 'ad_728x90', 'zoneId' => 452),
array('type' => 'openx', 'domId' => 'ad_300x250', 'zoneId' => 449),
array('type' => 'openx', 'domId' => 'ad_l2_300x250', 'zoneId' => 394),
),
);
?>
<?php
require_once 'ADLoader.class.php';
require_once 'ADConfig.php';
// 設置配置
ADLoader::setConfig($config, '/js/ADLoader.js');
// 加載廣告 (頻道: case_openx, 間隔: 500ms, 異步模式)
echo ADLoader::load('case_openx', 500, true);
?>