Consuming “Dallas” Data with PHP

Given that MIX10 is happening this week and that Microsoft Codename “Dallas” (CTP2) was just released, I thought it would be appropriate to look at how to consume “Dallas” data with PHP. (If you are at MIX10 and want more information, the “Dallas” session information is here.) In this post I’ll build a simple web page that displays some of the free “Dallas” data. And, I must say, I was really surprised at how simple and easy this entire process was.

What is “Dallas”?

What is Microsoft Codename “Dallas”, you ask? Here’s the official description from the “Dallas” page on the Windows Azure website:

Microsoft® Codename "Dallas" is a new service allowing developers and information workers to easily discover, purchase, and manage premium data subscriptions in the Windows Azure platform. Dallas is an information marketplace that brings data, imagery, and real-time web services from leading commercial data providers and authoritative public data sources together into a single location, under a unified provisioning and billing framework. Additionally, Dallas APIs allow developers and information workers to consume this premium content with virtually any platform, application or business workflow.

OK…so “Dallas” is…

  1. Data. (Some of it is free, some of it is “pay-as-you-go”. Check out the catalog of available data sets here: https://www.sqlazureservices.com/Catalog.aspx)
  2. Built on the Windows Azure Platform.
  3. Consumable via REST-based APIs.

That’s it. Simple.

How do I sign up for “Dallas”?

To sign up for “Dallas” you’ll need a Windows Azure subscription. Right now, it looks like the majority (maybe all?) of the data feeds are free, so it won’t cost you anything to sign up. If you don’t already have a Windows Azure subscription, you can create one here: https://www.microsoft.com/windowsazure/offers/. (You’ll need a Windows Live ID to sign up.) Then visit the catalog page and subscribe to any of the many available datasets. After you subscribe, you can see exactly what type of data is available in the feed by clicking on “Click here to explore the dataset”:

image

Also note that on the data preview page you will see your Account Key, Unique User ID and service URL. Make a note of these as you’ll need them later.

image

So far, pretty straightforward.

How to consume “Dallas” data with PHP

To consume “Dallas” data with PHP, you need to be able to send an HTTP request. I used the HTTP_REQUEST package available from PEAR (PHP Extension and Application Library), but any library that allows you to send an HTTP request should do the trick. (Note: I used the HTTP_REQUEST library because I already had it installed. If you are installing a new PEAR package, you may want to consider the HTTP_REQUEST2 package.) All you need to do is set the base URL and add your Account Key and Unique User ID as headers. So, to form a request, send it, and dump the output to the browser, my code looks like this (note the use of single quotes so strings preceded by $ are not interpreted as PHP variables):

Note: Since this is over HTTPS and I’m assuming PHP is running on a secure server, I’m not concerned about sending my Account Key and Unique User ID as request headers.

<?php
require_once "HTTP/Request.php";

$req =& new HTTP_Request("https://api.sqlazureservices.com/APService.svc/Categories/32555?ContentOption=0&NumItems=10&$format=atom10");
$req->addHeader('$accountKey', "Your_Account_Key");
$req->addHeader('$uniqueUserID', "Your_Unique_User_ID");
$req->sendRequest();
$parsed_xml = simplexml_load_string($req->getResponseBody());

print("<pre>");
print_r($parsed_xml);
print("</pre>");
?>

Again, simple.

A Simple Web Page

This web page consumes data from the Associated Press data feed and allows you to drill down and select a specific story. Certainly nothing fancy, but hopefully enough to get the idea across…

<html>
<head></head>
<body>
<?php
require_once "HTTP/Request.php";
define('CATAGORIES_URL', 'https://api.sqlazureservices.com/APService.svc/Categories');
define('ACCOUNT_KEY', "Your_Account_Key");
define('UNIQUE_USER_ID', "Your_Unique_User_ID");

$req =& new HTTP_Request();
$req->addHeader('$accountKey', ACCOUNT_KEY);
$req->addHeader('$uniqueUserID', UNIQUE_USER_ID);

if(isset($_GET['CategoryID']))
{
$req->setURL(CATAGORIES_URL.'/'.$_GET['CategoryID'].'?ContentOption=0');
$req->sendRequest();
$category = simplexml_load_string($req->getResponseBody());
foreach($category->entry as $story)
{
$storyLink = $story->link['href'];
echo "<a target='_blank' href='".$storyLink."'>$story->title</a></br>";
}
}
else
{
$req->setURL(CATAGORIES_URL);
$req->sendRequest();
$categories = simplexml_load_string($req->getResponseBody());
foreach($categories->entry as $category)
{
$categoryId = trim(strrchr($category->id, ':'),':');
echo "<a href="."'?CategoryID=".$categoryId."'>$category->title</a></br>";
}
}
?>
</body>
</html>

That’s it. Thanks.

-Brian