GetAd($publisher_id, $content_url)) {
//
// the ad data is in the
// ad_title, ad_url, ad_desc, ad_site properties
//
}
*/
//------------------------------------------------------------------------------
class AdSenseGetter
{
var $content_url;
var $publisher_id;
var $ad_title;
var $ad_url;
var $ad_desc;
var $ad_site;
//--------------------------------------------------------------------------
function AdSenseGetter()
{
$ad_title = "";
$ad_url = "";
$ad_desc = "";
$ad_site = "";
}
//--------------------------------------------------------------------------
//
// Return true if we got an ad, false if we didn't
// If we succeed in getting the ad, it'll be located in
// our ad properties...
//
function GetAd($publisher_id, $content_url)
{
$this->ad_title = "";
$this->ad_url = "";
$this->ad_desc = "";
$this->ad_site = "";
$url = $this->buildAdSenseUrl($publisher_id, $content_url);
$data = $this->getAdSenseAd($url);
if ($data) {
//
// This parsing code could probably be a lot simpler if we used regex,
// but that's not my strong point, so just do a bruteforce parse here.
//
//
// parse data now... use cheesy parser :-)
// first, jump ahead to
//
$data = strstr($data, 'ad_url = "http://pagead2.googlesyndication.com" . substr($data, 0, $i - 1);
//
// Now lets grab the ad title
//
$data = strstr($data, '">');
if ($data === FALSE) return false;
// trim off leading ">
$data = substr($data, 0, -2);
$i = strpos($data, '');
if ($i === FALSE) return false;
$this->ad_title = strip_tags(substr($data, 2, $i - 2));
//
// Now grab ad description
//
$data = strstr($data, '');
if ($data === FALSE) return false;
$i = strpos($data, '
');
if ($i === FALSE) return false;
$this->ad_desc = strip_tags(substr($data, 1, $i - 1));
//
// Finally, grab our ad site
//
$data = strstr($data, '');
// trim off leading >
$data = substr($data, 0, -1);
if ($data === FALSE) return false;
$i = strpos($data, '');
if ($i === FALSE) return false;
$this->ad_site = strip_tags(substr($data, 1, $i - 1));
return true;
}
return false;
}
//--------------------------------------------------------------------------
//
// Returns the specially formatted url that is used to get ads from AdSense
// For now, we hard code a small ad size 125x125 because we just want one
// ad. Could easily be expanded to get more than one ad.
//
function buildAdSenseUrl($publisher_id, $content_url)
{
$url = "http://pagead2.googlesyndication.com/pagead/ads?";
// add our publisher id
$url .= "client=" . $publisher_id . "&";
// add our date. In JS, this is Date object, getTime()
// in PHP, we use getTime()
$url .= "dt=" . time() . "&";
// add our last modified time here.
// hmm... how does google use this? perhaps to signal a re-index?
// for now, just use time()
$url .= "lmt=" . time() . "&";
// format, use 125x125 because it gives us one ad only
$url .= "format=125x125_as&";
// output. not sure what this does.
$url .= "output=html&";
$url .= "ad_type=text&";
// add our url here
$url .= "url=" . urlencode($content_url) . "&";
$url .= "loc=" . urlencode($content_url) . "&";
// some final parameters that don't really matter in our context
// should probably change timezone in future
$url .= "u_h=1024&u_w=1280&u_ah=1024&u_aw=1280&u_cd=32&u_tz=-420&u_his=1&u_java=false";
return $url;
}
//--------------------------------------------------------------------------
//
// Grab the raw data from AdSense. Uses curl, but could easily be mod'ed
// to use another method, raw sockets, etc.
//
function getAdSenseAd($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$data = curl_exec($ch);
curl_close($ch);
if (!($data === FALSE)) {
return $data;
} else {
return false;
}
}
//--------------------------------------------------------------------------
}
?>