php 廣告加載類,支持異步與同步加載。需要使用Jquery
ADLoader.class.php
<?php
/** 廣告加載管理類
* Date: 2013-08-04
* Author: fdipzone
* Ver: 1.0
*
* Func:
* public load 加載廣告集合
* public setConfig 廣告配置
* private getAds 根據channel創建廣告集合
* private genZoneId zoneid base64_encode 處理
* private genHtml 生成廣告html
* private checkBrowser 檢查是否需要同步加載的浏覽器
*/
class ADLoader{ // class start
private static $_ads = array(); // 廣告集合
private static $_step = 300; // 廣告加載間隔
private static $_async = true; // 是否異步加載
private static $_config = array(); // 廣告設置文件
private static $_jsclass = null; // 廣告JS class
/** 加載廣告集合
* @param String $channel 欄目,對應config文件
* @param int $step 廣告加載間隔
* @param boolean $async 是否異步加載
*/
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();
}
/** 設置config
* @param String $config 廣告配置
* @param String $jsclass js class文件路徑
*/
public static function setConfig($config=array(), $jsclass=''){
self::$_config = $config;
self::$_jsclass = $jsclass;
}
/** 根據channel創建廣告集合
* @param String $channel 欄目
*/
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'];
}
}
/** zoneid base64_encode 處理 */
private static function genZoneId(){
// 同步加載廣告不需要處理zoneid
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 */
private static function genHtml(){
$ads = self::$_ads;
$html = array();
if(self::$_jsclass!=null && $ads){
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("rn", $html);
}
/** 判斷是否需要強制同步加載的浏覽器 */
private static function checkBrowser(){
$user_agent = $_SERVER['HTTP_USER_AGENT'];
if(strstr($user_agent,'MSIE')!=''){
return false;
}
return true;
}
} // class end
?>
ADConfig.php
<?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
),
),
);
?>查看本欄目