«

»

Feb
18

My Modified AdMob PHP Class

I used in my code a modified version of Admob code that you can find the original on admob website. The idea that I never use print inside my applications, so i wrote it as a PHP class, the original code is a little bit modified. To use it simply replace $admobs variable with your own and enjoy!

/**
* Mobile Advertising PHP class
*
*/
class mobileads {

var $admobs = ‘a1458adcf8cca66′;

/**
* Return Admob code – Original code from admob.com
*
* @param mob_mode change mode from “test” to “live” when you are done testing
* @param mob_alternate_link use this to set a default link to appear if AdMob does not return an ad.
*/
function admob($mob_mode = ‘test’, $mob_alternate_link = ”) {

global $_SERVER;

//used for ad targeting
$mob_ua = urlencode(getenv(“HTTP_USER_AGENT”));
$mob_ip = urlencode($_SERVER['REMOTE_ADDR']);

if ($mob_mode==’test’)
$mob_m = “&m”;

$mob_url = ‘http://ads.admob.com/ad_source.php?s=’.$this->admobs.’&u=’.$mob_ua.’&i=’.$mob_ip.$mob_m;

$mob_contents = implode(”,@file($mob_url));

$mob_link = explode(“><”,$mob_contents);

$mob_ad_text = $mob_link[0];
$mob_ad_link = $mob_link[1];

if (isset($mob_ad_link) && ($mob_ad_link !=”)) {
//display AdMob Ad
$result = ‘‘. $mob_ad_text . ‘‘;
}
else {
//no AdMob ad, display alternate
$result = $mob_alternate_link;
}

return $result;
}

}