YQL Code Examples

Making YQL Queries with JavaScript

Summary

This code example shows you how to make YQL queries with JavaScript and the OpenSocial function makeRequest. The YQL query in the code example will get data from the GeoPlanet API based on the user's input.

OpenSocial Method: makeRequest

By using the OpenSocial method gadgets.io.makeRequest , you can make calls to a Web service with JavaScript without using a crossdomain.xml file. The calls in this example use 2-legged OAuth authorization.

Before calling the function makeRequest, define the base URI of the YQL Web service, the YQL query, and a callback function to handle the response, as shown below:

To make a call to the YQL Web service, you create a URL-encoded query string with the YQL query and the requested format type of the response and append this query string to the base URI of the YQL Web service. The code snippet below shows the base URI with the appended query string for the YQL Web service. Note that the spaces have been URL-encoded.

This code example has the JavaScript function toQueryString that creates the query string for the call to the YQL Web service as seen below:

By using a closure in the code below, the variable query, which holds the YQL query, is available to makeRequest. The function toQueryString then builds the query string, which is appended to the base URI by makeRequest before making the call to the YQL Web service.

In the code snippet, the function object runQuery is passed the parameters for makeRequest to call the YQL Web service. The handler returns the response data (JSON object) to the OpenSocial function stringify to be converted to a string so it can be displayed in a div tag.

Source Code

Making YQL Queries with PHP

Summary

This example, yql_php.php, is a simple application that uses cURL to make YQL calls to the Upcoming API.

To understand the following material, you should already be familiar with the topics covered in the Two-Minute Tutorial.

Building the YQL URL

To build the YQL URL, append the YQL query to the base URL of the YQL Web service.

  1. First, assign the base URL of the YQL Web service to a variable.

  2. Now create the YQL query to the Upcoming API and append it to the YQL base URL. YQL queries are passed as query strings, so they must be URL encoded. The query string must begin with q=.

  3. The YQL Web service returns XML as the default format. This code example requests JSON by appending the name-value pair format=json to the query string as shown.

  4. Run the query in the YQL console and look at both the URL in The REST query window and the JSON response in the Formatted View window.
Calling the YQL Web Service with cURL

Calling the YQL Web service with cURL only requires three lines of code. After initializing the call by passing the curl the YQL URL, you request a response with curl_setopt and then execute the call.

To make the response easier to handle, convert the JSON response to a PHP object to easily access data.

Parsing the Response

The YQL Web service will always return results wrapped in the query field with meta data. If the YQL query returns data, the data is wrapped in the results field, which you can see by running this query.

If no data is returned, the results element in the XML response is empty or the results field in JSON is null. Therefore, before parsing the response returned by YQL, your code should always check the results field as shown below:

The structure of the data within the results field that is returned by YQL is different for each API. The repeated field (like a row in an SQL table) in the returned response in this code example is the event field. The code below extracts and displays data from each event:

Source Code

Data Scraping with YQL

Summary

This example, yql_html_scraper.html, uses YQL to scrape HTML from Yahoo News and then creates customized output. The YQL module for YUI is used to call the YQL Web service.

Using the YQL Module for YUI

You can can download the YQL module for YUI from GitHub or include it in your Web page like any other JavaScript file. The steps below will show you how to use the YQL module to call the YQL Web service.

  1. Include the YUI library, which includes the YQL module, in your Web page.

  2. Create a YUI instance to load the Node and YQL modules.

  3. Using the html table, create a YQL query to get data from Yahoo News. See Using XPath in YQL Statements for details about the YQL query.

  4. Instantiate a YQL object by passing the YQL query and a callback function to Y.YQL.

  5. To initiate the call to the YQL Web service, add an event handler to a click event that will call the YQL.

  6. Make sure that results were returned and then parse those results.

    Because each a element is a seen as row of data by YQL, you need to iterate through each a element to access the href and content child elements. Run the query in the YQL Console to examine the data structure or the returned response in greater detail.

Using XPath in YQL Statements

The YQL statement below is used in this code example to access the links for stories from Yahoo News. This section will examine the data source for the YQL statement and the XPath expression that extracts the links.

select * from html where url='http://news.yahoo.com/{category}' and xpath='//div[@class="content"]//div[@class="txt"]/p'

The YQL query in this example allows you to access HTML using the html table. YQL also has tables for accessing other data formats, such as XML, CSV, JSON, RSS, XST, etc.

select * from html

The key url determines the data source for the YQL query. The data source in this example is World News page on Yahoo News.

url='http://news.yahoo.com/world'

The XPath statement below extracts the <p> elements from <div> element with the class txt. Each paragraph (p element) is returned as a row (repeated element in XML).

xpath='//div[@class="content"]//div[@class="txt"]/p'

For more information about XPath, see WC3 XPath Language.

Source Code

Table of Contents