PHP class for using the Google Analytics APIPHP class for using the Google Analytics API

Posted April 23rd, 2009 in PHP (Updated April 28th, 2009)

Just quickly following up my post from earlier on today where I looked at how to log into the Google Analytics API using PHP and CURL using Username/Password Authentication I wrapped the login code up into a class with methods for logging in and then calling API functions. I'll look at parsing the XML result data at a later stage.

Update April 28th 2009: I've started a series about this. The full class, which I will continue to update over the next few days, can be downloaded here. The class presented below is an earlier version of it. Original post continues...

Here's the class:

class analytics_api {

    protected $auth;
   
    public function login($email, $password) {
       
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://www.google.com/accounts/ClientLogin");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, true);
       
        $data = array(
            'accountType' => 'GOOGLE',
            'Email' => $email,
            'Passwd' => $password,
            'service' => 'analytics',
            'source' => ''
        );
       
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $output = curl_exec($ch);
        $info = curl_getinfo($ch);
        curl_close($ch);
       
        $this->auth = '';
        if($info['http_code'] == 200) {
            preg_match('/Auth=(.*)/', $output, $matches);
            if(isset($matches[1])) {
                $this->auth = $matches[1];
            }
        }
       
        return $this->auth != '';
   
    }
   
    public function call($url) {

        $headers = array("Authorization: GoogleLogin auth=$this->auth");
       
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        $output = curl_exec($ch);
        $info = curl_getinfo($ch);
        curl_close($ch);

        if($info['http_code'] == 200) {
            return $output;
        }
        else {
            return false;
        }
       
    }
   
}

And here's an example using it, calling https://www.google.com/analytics/feeds/accounts/default which gets a list of all the accounts that are available to view data for.

$api = new analytics_api();
if($api->login('email@example.com', 'my-secret-password')) {
    $xml = $api->call('https://www.google.com/analytics/feeds/accounts/default');
    // ... process the xml ...   
}
else {
    // login failed   
}

The class could easily be extended to parse the XML for common functions and have a function for getting the list of accounts into an array (or similar) and so on for other functions.

Update April 28th 2009: I've started a series about this and created a PHP class. The full class, which I will continue to update over the next few days, can be downloaded here.

Related posts:

Comments

blog comments powered by Disqus