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”?
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…
- 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)
- Built on the Windows Azure Platform.
- 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: http://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”:
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.
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.
$req =& new HTTP_Request(“https://api.sqlazureservices.com/APService.svc/Categories/32555?ContentOption=0&NumItems=10&$format=atom10“);
$parsed_xml = simplexml_load_string($req->getResponseBody());
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…
$req =& new HTTP_Request();
$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>”;
$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>”;
That’s it. Thanks.