Zend framework: Working with Gdata

October 9, 2009

In this post, I will talk about working with the open source zend framework and Google Health API . The zend framework offers an elegant method to access health data and post notices. I will talk about using the ClientLogin method first. ClientLogin method is recommended for desktop applications by Google. This post is based on the zend framework documentation and Google health API documentation.

The zend packages are available from Fedora repositories.

Authentication

Authentication is the first step in working with Gdata. The ClientLogin method uses standard username and password for authentication. We will be using the developers sandbox (https://www.google.com/h9) for our testing. The authentication code could be:


function authenticate()
{
$user = "user@gmail.com";
$pass = "passwd";
$h9service = Zend_Gdata_Health :: H9_SANDBOX_SERVICE_NAME;
$client = Zend_Gdata_ClientLogin :: getHttpClient($user, $pass, $h9service);
return $client;
}

This code creates a http client using the ClientLogin mechanism for the H9 sandbox service and returns the client. This should be your first step.

Health Service

The next step is to create a Google health service client from the http client. This could be the code:


$client = authenticate();
$useH9 = true;
$healthService = new Zend_Gdata_Health($client, "MyService", $useH9);

In this code block we call the authenticate() function to get the http client and create a health service client.

Profile List Feeds

ClientLogin Mechanism requires that we extract the profile ID for subsequent queries. It provides profile list feed from which we extract the profile we want.

The register feed is:

https://www.google.com/health/feeds/register/ui/profileID

The profile feed is:

https://www.google.com/health/feeds/profile/ui/profileID

Since one user can have more than one profile ID in Google Health, we need to extract the profile ID we need. For ClientLogin mechanism, we need to set the profile ID for the health service client. In the following code, we extract the profile from profile list feed and set the first profile ID for the health service:


$feed = $healthService->getHealthProfileListFeed();
$entries = $feed->getEntries();
$profileID = $feed->entry[0]->getProfileID;
$healthService->setProfileID->($profileID)

In the code above, we use the profile list feed to enlist all the profiles for a particular user. This feed is available only under ClientLogin authentication mechanism.

Feeds and Queries

Now that we have the profile ID, we can get all the CCR entries from the feed. To do so, we construct a query and use that query to get all the feed entries


$query = new Zend_Gdata_Health_Query("https://www.google.com/h9/feeds/profile/ui/$profileID");
$profileFeed = $healthService->getHealthProfileFeed($query);
foreach ($profileFeed->getEntries() as $entry) {
$ccr = $entry->getCcr();
echo '<p>' . $ccr->getXML() . '</p>';
$count++;
}

In the code above, we just print the CCR entries in XML format. If you want to print text of, say medication, you could use this code:


$query = new Zend_Gdata_Health_Query(SCOPE . "profile/ui/$profileID");
$query->setDigest("true");
$profileFeed = $healthService->getHealthProfileFeed($query);
$entries = $profileFeed->getEntries();
foreach ($entries as $entry) {
$medications = $entry->getCcr()->getMedications();
foreach($medications as $med) {
$xpath = new DOMXpath($med->ownerDocument);
$elements = $xpath->query("//ccr:Medications/ccr:Medication/ccr:Product/ccr:ProductName/ccr:Text");
foreach ($elements as $element) {
echo $element->nodeValue . '<br>';
}
}
}

In the code, above we use setDigest to get the entire feed as one entry. Hence it will loop only once here. We then use DOMXpath object to query the CCR text. We could get text of other categories this way.

Sending Notices

You can send notices to the profile which can be as simple as a text message or can optionally include a CCR element. I will just point out that it is very simple to send these. It is just one call.


$subject = "This is my subject";
$body = " This is the message ";

$healthService->sendHealthNotice($subject, $body);

Its as simple as that.

This concludes this post. In the next post, I will talk about other methods of authentication. This post should hopefully help anyone to get started on the Gdata API immediately.