Thursday, September 10, 2009

Google Authetication Using Curl

First things first, in order to access the analytics data for the
intended profile you need to login using an API request. This basically
involves using your Google Account login e-mail and password. Using the
PHP class, this was a simple function called login() which took those parameters. Here is an adaption of the code used in this function from the class:

$ch = curl_init("https://www.google.com/accounts/ClientLogin");
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);

$auth = '';
if($info['http_code'] == 200) {
preg_match('/Auth=(.*)/', $output, $matches);
if(isset($matches[1])) {
$auth = $matches[1];
}
}

Once the login request is processed, we are given an authorization key
to use when requesting data ($auth), which gets set as an cURL option for
later requests, such as the requests for data, for session security purposes.

Hope this code helps.....

No comments:

Post a Comment