I’ve been messing with the Foursquare API and I was getting frustrated by it’s unreadable output. Too much XML, not enough whitespace!
[email protected]:~$ curl -l http://api.foursquare.com/v1/venues?geolat=41.243385\&geolong=-96.017697
170922 Peter Kiewit Institute 79037 College & Education:University University http://foursquare.com/img/categories/education/default.png 1110 South 67th Streetat Pacific St Omaha NE 68182 41.2436984 -96.0146702 0 4025543333 PKI 255 345883 Liv Lounge 79168 Nightlife:Lounge Lounge http://foursquare.com/img/categories/nightlife/lounge.png 2279 S 67th Stat Center St Omaha Nebraska 68106 41.2436984 -96.0146702 0 255 245578 Walmart 79221 Shops:Department Store Department Store http://foursquare.com/img/categories/shops/default.png 1606 S 72nd Stat Pine St Omaha NE 68124 41.2440158 -96.0241158 0 4023939560 541 254875 Aksarben Village 79196 Parks & Outdoors:Plaza / Square Plaza / Square http://foursquare.com/img/categories/parks_outdoors/default.png 6600 Shirley Stat Center St Omaha NE 68106 41.2414632 -96.0120519 0 518 170916 Scott Technology Center 79115 Home / Work / Other:Corporate / Office Corporate / Office http://foursquare.com/img/categories/building/default.png 6825 Pine StreetOmaha NE 68106 41.243385 -96.017697 0 4025026000 0 2468550 Juice Stop 79071 Food:Juice Bar Juice Bar http://foursquare.com/img/categories/food/default.png 2119 S 67th StShirley St Omaha NE 68106 41.2403021 -96.0146607 0 4029343870 427 199022 Scott Conference Center 96353 Home / Work / Other:Event Space Event Space http://foursquare.com/img/categories/building/default.png 6450 Pine StreetOmaha NE 41.244514 -96.013489 0 373 772128 Courtyard Aksarben VIllage 79273 Travel:Hotel Hotel http://foursquare.com/img/categories/travel/hotel.png 1625 S 67th Stat Pine St Omaha NE 68106 41.243304 -96.014349 0 4029514300 280 247344 Wohlner's Market 96359 Shops:Food & Drink:Gourmet Gourmet http://foursquare.com/img/categories/shops/default.png 2289 S 67th Stat Center St Omaha NE 68106 41.23806024488222 -96.0138988494873 0 4025516875 672 2564306 First Data 79115 Home / Work / Other:Corporate / Office Corporate / Office http://foursquare.com/img/categories/building/default.png 6855 Pacific StreetOmaha NE 68106 41.2487029 -96.0192553 0 606
[email protected]:~$
How can we rectify this situation? xmllint! This is part of libxml and should exist on most systems.
[email protected]:~$ curl -l http://api.foursquare.com/v1/venues?geolat=41.243385\&geolong=-96.017697 | xmllint --format -
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
101 5265 101 5265 0 0 13778 0 --:--:-- --:--:-- --:--:-- 29914
170922
Peter Kiewit Institute
79037
College & Education:University
University
http://foursquare.com/img/categories/education/default.png
1110 South 67th Street
at Pacific St
Omaha
NE
68182
41.2436984
-96.0146702
0
4025543333
PKI
255
... snip ...
2564306
First Data
79115
Home / Work / Other:Corporate / Office
Corporate / Office
http://foursquare.com/img/categories/building/default.png
6855 Pacific Street
Omaha
NE
68106
41.2487029
-96.0192553
0
606
[email protected]:~$
So there you go, a handy trick of the day for you.
In case you missed it the short form is:
curl -l [url] | xmllint --format -
Comments
If you don’t mind me asking, why are you using the XML API instead of the JSON API? I’ve all but abandoned XML for just about everything. It’s an absolute pain to work with.
I’m not using it in production, but I like it for reading in a shell. Visually I don’t grok JSON as fast as I do XML, but that may just be me.
As for working with it, SimpleXML (http://www.php.net/simple_xml) in PHP is really great for small documents. And if you are handling RSS or ATOM then SimplePie (http://simplepie.org/) is an awesome all in one solution.
Likewise in Python lxml (http://codespeak.net/lxml/) and Universal Feed Parser (http://www.feedparser.org/)