How to Get Google Voice Call Length in PHP (Google Voice API)

December 8, 2009

I am interested in building a google voice / highrise mashup that tags people by how long overall I’ve talked to them. I ran across this bit of PHP code by Aaronpk that attempts to use the main voice functionality as an API. And it works really well.

The below is adapted from that code to get all calls and get the duration of the calls. Of course, duration is approximate, but that’s as good as it is going to get for me.

$login = 'username';
$pass = 'password';

$cookieFile = '/tmp/gvCookies.txt';

$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)");

// fetch the login page
curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/accounts/ServiceLogin?passive=true&service=grandcentral');
$html = curl_exec($ch);

if(preg_match('/name="GALX"\s*value="([^"]+)"/', $html, $match))
	$GALX = $match[1];
else
	throw new Exception('Could not parse for GALX token');

curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/accounts/ServiceLoginAuth?service=grandcentral');
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
	'Email' => $login,
	'Passwd' => $pass,
	'continue' => 'https://www.google.com/voice/account/signin',
	'service' => 'grandcentral',
	'GALX' => $GALX
	));

// Process login
$html = curl_exec($ch);
if( preg_match('/name="_rnr_se".*?value="(.*?)"/', $html, $match) )
{
	$rnr_se = $match[1];
}
else
{
	exit("Could not log in to Google Voice with username: " . $login);
}

// Get results for all recent calls, paginated of course
curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/voice/inbox/recent/all/');
curl_setopt($ch, CURLOPT_POST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$xml = curl_exec($ch);

$dom = new DOMDocument();

// load the "wrapper" xml (contains two elements, json and html)
$dom->loadXML($xml);
$json = $dom->documentElement->getElementsByTagName("json")->item(0)->nodeValue;
$json = json_decode($json);

// now make a dom parser which can parse the contents of the HTML tag
$html = $dom->documentElement->getElementsByTagName("html")->item(0)->nodeValue;
// replace all "&" with "&" so it can be parsed
$html = str_replace("&", "&", $html);
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);

$calls = array();

// Call duration isn't included, so foreach $json->messages, getting gc-message-call-details for each element
foreach( $json->messages as $mid=>$convo )
{
	//find this conversation by message id
	$elements = $xpath->query("//div[@id='$mid']");
	if(!is_null($elements))
	{
		$element = $elements->item(0);
		//find the gc-message-call-details contents
		$XMsg = $xpath->query("//span[@class='gc-message-call-details']", $element);
		//add details to the convo
		$convo->details = $XMsg->item(0)->nodeValue;
	}
	//add $convo to results
	$calls[] = $convo;
}

foreach($calls as $mid => $convo)
{
	echo "Call $mid lasted $convo->details\n";
}

Stay in Touch!

Subscribe to our newsletter.

Solutions Architecture

browse through our blog articles

Blog Archive