Simple way to get adsense earning from google adsense api using php, get adsense earning easily and without complexity in php curl.

How To Get AdSense Earning Using API

Using Google AdSense API and Qassim_HTTP() Function, we will get adsense earning easily.

Create A Project

Firstly, go to Google Developer Console and Create a project, we need “redirect uri” and “client id” and “client secret“, read this tutorial.

Qassim_HTTP() Function

This is function to use PHP cURL easily:

function Qassim_HTTP($method, $url, $header, $data){ /* By Qassim Hassan, wp-time.com */
    if( $method == 1 ){
        $method_type = 1; // 1 = POST
    }else{
        $method_type = 0; // 0 = GET
    }

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($curl, CURLOPT_HEADER, 0);

    if( $header !== 0 ){
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
    }

    curl_setopt($curl, CURLOPT_POST, $method_type);

    if( $data !== 0 ){
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }

    $response = curl_exec($curl);
    $json = json_decode($response, true);
    curl_close($curl);

    return $json;
}

Login URL Function

This is function to get login link with google adsense:

function Get_Login_URL(){ /* By Qassim Hassan, wp-time.com */
    $scope = "https://www.googleapis.com/auth/adsense"; // Do not change it!

    $redirect_uri = "xxxx"; // Enter your redirect uri

    $client_id = "xxxx"; // Enter your client id

    $client_secret = "xxxx"; // Enter your client secret

    $login_url = "https://accounts.google.com/o/oauth2/v2/auth?scope=$scope&response_type=code&redirect_uri=$redirect_uri&client_id=$client_id";

    return $login_url;
}

Google Adsense Authorization

This is fucntion to request authorization:

function Google_Adsense_Authorization($code){ /* By Qassim Hassan, wp-time.com */
    $redirect_uri = "xxxx"; // Enter your redirect uri

    $client_id = "xxxx"; // Enter your client id

    $client_secret = "xxxx"; // Enter your client secret

    $header = array( "Content-Type: application/x-www-form-urlencoded" );

    $data = http_build_query(
        array(
            'code' => str_replace("#", null, $code),
            'client_id' => $client_id,
            'client_secret' => $client_secret,
            'redirect_uri' => $redirect_uri,
            'grant_type' => 'authorization_code'
        )
    );

    $url = "https://www.googleapis.com/oauth2/v4/token";

    $result = Qassim_HTTP(1, $url, $header, $data);

    return $result['access_token']; // Your access token will be finished after 1 hour.
}

Get Earnings Function

This is function to get adsense earning:

function Get_Earnings($access_token, $startDate, $endDate){ /* By Qassim Hassan, wp-time.com */
    $header = array( "Authorization: Bearer $access_token" );

    $id = "xxxx"; // Enter your google adsense ID, for example: pub-5465656165485

    $url = "https://www.googleapis.com/adsense/v1.4/accounts/$id/reports?startDate=$startDate&endDate=$endDate&metric=EARNINGS";

    $result = Qassim_HTTP(0, $url, $header, 0);

    return $result;
}

Display Adsense Earnings

Now we will display google adsense earnings:

/* By Qassim Hassan, wp-time.com */
if( isset($_GET['code']) and !isset($_SESSION['access_token']) ){
    $code = $_GET['code'];

    $access_token = Google_Adsense_Authorization($code);

    $_SESSION['access_token'] = $access_token;
}

if( isset($_SESSION['access_token']) ){
    $access_token = $_SESSION['access_token'];

    $startDate = "2015-12-12"; // Change date YYYY-MM-DD
    $endDate = "2015-12-12"; // Change date YYYY-MM-DD
    $earnings = Get_Earnings($access_token, $startDate, $endDate);

    //print_r($earnings); // You can print result array
    if( !empty($earnings['error']) ){ // if access token time has been finished
        $login_url = Get_Login_URL();
        echo '<a href="'.$login_url.'">Re login</a>'; // re login to get new access token
    }else{
        echo $earnings['totals'][0]; // Display earnings of date 2015-12-12
    }
}else{
    $login_url = Get_Login_URL();
    echo '<a href="'.$login_url.'">Login</a>';
}

Note

In $startDate and $endDate enter date to get earning of day by date, you can get total earnings of two days or more, for example:

$startDate = "2015-12-11"; // date format YYYY-MM-DD
$endDate = "2015-12-12"; // date format YYYY-MM-DD
$earnings = Get_Earnings($access_token, $startDate, $endDate);

echo $earnings['totals'][0]; // Display total earnings of two days