ก๊วนซอฟท์แวร์ </softganz> SoftGang (Gang Software)

Web &amp; Software Developer Gang.

PHP : How to show Adsense on web

by Little Bear @18 ก.ค. 55 09:16 ( IP : 122...25 ) | Tags : PHP

วิธีเอาข้อมูลจาก Google Adsense มาแสดงบนหน้าเว็บของเรา

<?php
///////////////////////////////////////////////
// Adsense Earnings and Statistics
// Script by: Gary http://www.garyshood.com
// Do whatever you want with it (BSD License)
///////////////////////////////////////////////

$username = "username@gmail.com";
$password = "password";

///////////////////////////////////////////////
// Curl Get
///////////////////////////////////////////////

function curl_get($url, $cookiefile) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_COOKIEJAR, $cookiefile);
    curl_setopt($curl, CURLOPT_COOKIEFILE, $cookiefile);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    $data = curl_exec($curl);
    curl_close($curl);
    return $data;
}

///////////////////////////////////////////////
// Curl Post
///////////////////////////////////////////////

function curl_post($url, $cookiefile, $post) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_COOKIEJAR, $cookiefile);
    curl_setopt($curl, CURLOPT_COOKIEFILE, $cookiefile);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    $data = curl_exec($curl);
    curl_close($curl);
    return $data;
}

///////////////////////////////////////////////
// Create the temporary cookie
///////////////////////////////////////////////

$cookiefile = tempnam("", "adsense_");

///////////////////////////////////////////////
// Get the GA3T value for login
///////////////////////////////////////////////

$data = curl_get("https://accounts.google.com/ServiceLoginBoxAuth", $cookiefile);

preg_match("/<input type=\"hidden\" name=\"GALX\" value=\"(.*?)\"/", $data, $galx);

///////////////////////////////////////////////
// Login to AdSense
///////////////////////////////////////////////

$data = curl_post("https://accounts.google.com/ServiceLoginBoxAuth", $cookiefile, "GALX=$galx[1]&Email=$username&Passwd=$password&PersistentCookie=yes&rmShown=1");

///////////////////////////////////////////////
// Check if login was successful
///////////////////////////////////////////////

if(strpos($data, 'Username and password do not match.')) {
    unlink($cookiefile);
    die("Login failed.\n");
}

///////////////////////////////////////////////
// Authenticate login
///////////////////////////////////////////////

$data = curl_get("https://www.google.com/accounts/CheckCookie?continue=https%3A%2F%2Fwww.google.com%2Fadsense%2Flogin-box-gaiaauth&followup=https%3A%2F%2Fwww.google.com%2Fadsense%2Flogin-box-gaiaauth&hl=en_US&service=adsense&ltmpl=login&chtml=LoginDoneHtml", $cookiefile);

///////////////////////////////////////////////
// Fetch AdSense statistics
///////////////////////////////////////////////

$fetch_times = array(
'today',
'yesterday',
'last7days',
'thismonth',
'lastmonth'
);

///////////////////////////////////////////////
// Loop for each time and gather results
///////////////////////////////////////////////

for($i = 0; $i < count($fetch_times); $i++) {
    switch($fetch_times[$i]) {
        case 'today':
            $post = "d=today&hl=en";
            break;
        case 'yesterday':
            $post = "d=yesterday&hl=en";
            break;
        case 'last7days':
            $post = "hl=en&ag=month";
            break;
        case 'thismonth':
            $post = "d=thismonth&ag=month&hl=en";
            break;
        case 'lastmonth':
            $post = "d=lastmonth&ag=month&hl=en";
            break;
    }

    $data = explode("\n", curl_post("https://www.google.com/adsense/v3/gwt/exportCsv", $cookiefile, $post));
    $data = mb_convert_encoding($data[count($data)-2], 'UTF-8', 'UTF-16');
    $match = str_getcsv($data, "\t");

    $impressions[$fetch_times[$i]] = $match[1];
    $clicks[$fetch_times[$i]] = $match[2];
    $ctr[$fetch_times[$i]] = $match[3];
    $ecpm[$fetch_times[$i]] = $match[5];
    $earnings[$fetch_times[$i]] = $match[6];
}

/*
* Display results
*
* Format: $statistic[time]
*
* Examples:
* Impressions Today: $impressions[today]
* Clicks Yesterday: $clicks[yesterday]
* CTR This Month: $ctr[thismonth]
* ECPM Last Month: $cpm[lastmonth]
* Earnings Last 7 Days: $earnings[last7days]
*/

echo "Today Earnings: $earnings[today]<br />";
echo "Impressions Today: $impressions[today]<br />";
echo "Clicks Yesterday: $clicks[yesterday]<br />";
echo "CTR This Month: $ctr[thismonth]<br />";
echo "ECPM Last Month: $cpm[lastmonth]<br />";
echo "Earnings Last 7 Days: $earnings[last7days]<br />";

/**
* Clean up cookie
*/

unlink($cookiefile);
?>

ที่มา garyshood.com