PHP : How to show Adsense on web
วิธีเอาข้อมูลจาก 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<mpl=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
Relate topics
- nginx - ถึงเวลาซะที
- PHP PDO using
- การเก็บ JSON ใน MySQL ด้วย PHP
- macOS 12 Monterey: Web Server Setup/Apache/MariaDB/PHP Multiple Versions
- Ubuntu Server Installation
- Bug : isset($body['location']) ดันเป็นจริงเมื่อ $body ไม่ใช่ array
- MVC - แนวคิดเกี่ยวกับ MVC
- FTP chmod
- Hash & Salt
- Regular Expression - ยังไง ยังไง ก็ใช้ไม่ถูกสักที
- PHP : Send SMS by smsgateway.com
- Directadmin PHP path
- Application Programming Interface (API)
- IP Camera snapshot
- Enable Exif
- Make Ubuntu to be a web server (LAMP)
- SELECT - OnChange
- Class fpdf กับภาษาไทย
- มาแล้ว PHP 5.3 พร้อมกับ error ที่ไม่คาดฝัน
- Hello Twetter from SoftGanz