Writing a Server-Side Proxy
Writing a Server-Side Proxy
Before JSONP became a popular option for writing client-side “mashups,” developers would traditionally write their own set of web services that would proxy the remote services. These web services would be deployed to the same domain that would serve the application, and this would mean no calls violated the same-origin policy. The implementation of such a solution will vary depending on the language you are working with, and the implementation details are beyond the scope of this book, but there is a lot of good information out there if this is something that you require.
Another alternative to writing your own server-side proxy, given that JSONP is now available to you, is to use a third-party service to wrap web services that provide only a JSON implementation with JSONP. A couple of good examples of this are the following:
http://jsonpify.heroku.com http://jsonproxy.appspot.com
Both services have source code available and are hosted in cloud-hosting services, so you can modify them to suit your own specific requirements.
Yahoo Query Language
Yahoo Query Language (YQL) is fantastic; there really aren’t enough nice things that can
be said about it. It is essentially a very robust and generic proxy that can be used to request data from varying sources and return it in a standardized XML or JSON (with JSONP support) format.
Interacting with YQL is done through the use of an SQL-like syntax, which is where the QL part of the name comes from. Figure 10–6 shows a screenshot of the developer console, which is one of the core tools of the YQL suite. The console can be found at http://developer.yahoo.com/yql/console.
CHAPTER 10: Integrating with Social APIs
Figure 10–6. The developer console is a great place to begin to understand the possibilities of YQL. One of the really nice things about YQL is its use of data tables. These data tables
provide YQL information on how to interact and obtain information from various services around the Web. When you first load the YQL console, you will probably see around 160 data tables initially available, and these relate mostly to tables that interface with other Yahoo! services. By accessing the community tables (which you can do by selecting the Show Community Tables link on the right), you gain access to almost 1,000 different tables that you can interact with, and both Foursquare and Gowalla tables are included in this set.
In Foursquare’s case, the following is a YQL query that is designed to interact with its venues API endpoint:
select * from foursquare.venues where geolat="33.7772869" and geolong="-84.3976068"
In addition to running requests through the console, you can execute YQL through a set of published web services. In each case, a REST query for the information is displayed at the bottom of the console interface. This REST query can then be copied and pasted into your own client-side JavaScript—for instance, directly into a jQuery ajax function call. We won’t include a sample URL here, as these are fairly unreadable, since they contain the escaped SQL statement as part of the query. It is fairly simple to recognize
CHAPTER 10: Integrating with Social APIs
an application using YQL though (using the developer tools of course), as it will be routing requests through http://query.yahooapis.com/v1/public/.
While it would be great to go into more depth on YQL, we won’t be using it to solve our current issue for accessing Foursquare or Gowalla (the reasons for this will be outlined very soon), so that would be counterproductive. However, it is definitely worth a look when you have the time, as it can definitely be extremely useful.