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

YQL Social Application

Summary

This example, yql_basic.php, is a simple application that uses the Yahoo! Social SDK for PHP to make YQL calls. Using YQL with the SDK is ideal because the SDK will handle your OAuth authorization, which is required to access the Social Directory APIs. YQL, in turn, extends the functionality of the SDK by enabling access to the public data from Yahoo! Web services and external data such as RSS and Atom feeds.

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

Initial Code

Before proceeding with the code that makes YQL queries, be sure to include the PHP SDK and define an API Key and a Shared Secret:

Querying Public Data

There are great number of sources for public data on the Web that can be accessed with YQL. Although public, many of these sources require authorization. With the API Key and the Shared Secret provided by Yahoo!, you can use the PHP SDK to perform two-legged authorization with the YahooApplication class. (If you need to read up on two-legged authorization, see Private Data v. Public Data.) From a YahooApplication object, invoke the method query, which calls the YQL Web service (that is, runs the YQL queries). The following code snippets show you how to perform two-legged authorization with YahooApplication and how to make YQL queries.

The API Key and Shared Secret are passed to the constructor of the YahooApplication class. If the application has been authorized by Yahoo!, the YahooApplication object $two_legged_app is returned:

Next, the application defines two YQL queries: one to the Flickr API, another to the Yahoo! News RSS feed. You can also run these queries in the YQL Console:

With the $two_legged_app object, the application calls the query method. The var_dump shows the structure and contents of the query response of the YQL Web service:

Querying Private Data

Unlike YQL queries for public data, you need user authorization to obtain data from the Social Directory APIs (Profiles, Updates, Connection, Contacts, and Presence). During the process of user authorization, the user will be redirected to the Yahoo! login page and then asked to authorize your application. You initiate this three-legged authorization process with the YahooSession class. (If you are unfamiliar with the term "three-legged authorization," you may want to read Private Data v. Public Data.) Getting a valid YahooSession object means that the user has authorized your application, allowing you to access this user's private data. The following code snippets show how to perform authorization with YahooSession and how to use YQL to access private social data from Yahoo!

The method requireSession makes sure the user has logged into Yahoo!, redirecting to the Yahoo! login page if necessary. A YahooSession object is returned, confirming that the OAuth authorization has been completed:

The YQL queries and API names are assigned to the associative array $api_queries:

The YQL query is made from the YahooSession object $session with the method query. The use of var_dump allows you to see the contents of the returned response for each YQL query:

The following listing shows the var_dump from the YQL query made on the Profiles API.

Source Code

YQL INSERT: WordPress Open Application

Summary

This code example shows how to use the YQL INSERT statement in an Open Application to post to your WordPress blog. YML is used to create a simple HTML form for the UI, and PHP is used to call the YQL Web service to run the YQL INSERT statement.

Small View: yml:form

The WordPress Open Application uses the YML tag yml:form for entering information. The attribute params is like the HTML form attribute action. In the code snippet below, the user triggers the call to the script yql_insert_wordpress.php when submitting the form by clicking "Publish".

The attribute insert places the returned response from yql_insert_wordpress.php into the div tag "blog_sect". If "Publish" is clicked again, the contents in this div will be replaced with the new content returned by yql_insert_wordpress.php.

YQL: INSERT

The syntax for the YQL INSERT statement follows that of the SQL INSERT statement:

INSERT INTO (table) (list of comma separated field names) VALUES (list of comma separated values)

Try running the following YQL INSERT statement that references the WordPress Open Table and then view the results at http://yqlblog.wordpress.com .

USE 'http://www.datatables.org/wordpress/wordpress.post.xml' AS wordpress.post; INSERT INTO wordpress.post(title, description, blogurl, username, password) VALUES ("YQL meets WordPress", "Posting with YQL", "http://yqlblog.wordpress.com", "yqlblog", "password")

Calling the YQL Web Service
Building the URL

The URL for making a request to the YQL Web service has a base URL and a query string. The query string contains the YQL statement, the requested response format, and any environment files. The environment file at http://datatables.org/alltables.env includes the USE statement for the WordPress Table, which creates the alias wordpress.post as seen below:

use 'http://www.datatables.org/wordpress/wordpress.post.xml' as wordpress.post;

Below, the URL used to call the YQL Web service is divided and stored in variables to illustrate the different components: $yql_base_url holds the base URL to the YQL Web service, $yql_insert holds the YQL INSERT statement, and $yql_format and $yql_env_tables hold the request format and URL to the environment file respectively.

Because the YQL INSERT statement is sent via POST with cURL, we build the POST fields by concatenating the various components. We configure the cURL call to send the POST fields in Calling the YQL Web Service. Notice that in this code snippet only $yql_format is not URL-encoded because it contains the characters '&' and '='.

Sending the Request to YQL

Before making the request with cURL, let's look again at the variables that hold the base URL and the post fields from Building the URL:

The cURL call for this code example is fairly typical. You need to configure cURL to send the request via POST and accept POST fields, ask for a returned response, and turn off SSL verification for both host and peer certificates:

Source Code
Small View Code

WordPress Open Application

Getting Updates with YQL

Summary

This code example shows you how to use YQL with the Yahoo! Social SDK for PHP to get user updates and the updates for the user's connections. The SDK handles the OAuth authorization, and YQL fetches the user data.

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

YQL Queries

The syntax of YQL is similar to that of SQL, and the YQL Web service, like a MySQL database, returns data based on queries. The YQL Web service, however, returns the data in the form of XML or JSON. For more information about making YQL queries, see SELECT Statement.

YQL has a special literal me that holds the GUID of the currently logged in Yahoo! user. This YQL query uses this special literal to get the data of five connections of the currently logged in Yahoo! user.

SELECT * FROM social.connections WHERE owner_guid=me LIMIT 5

From the YQL response for the query above, you can then extract the GUID for each connection. These GUIDs can then be matched against the key guid in a new YQL query to get the updates for each connection as shown in the code snippet below:

SELECT * FROM social.updates WHERE guid='GUID_of_a_connection' LIMIT 5

Now that you have some familiarity with YQL and its syntax, try using the YQL Console to experiment with different YQL queries and different tables such as Flickr and Search.

Calling the YQL Web Service with the PHP SDK

The code example uses the method requireSession from the class YahooSession to get authorization to access user data and the method query to call the YQL Web service. The code snippets below will focus on authorization and YQL queries.

This one line of code does a lot of work to complete the OAuth authorization. The method requireSession first looks for an existing session, and if it's not found, redirects the user to the Yahoo! login page to sign in. The user can then authorize the app to access private data. The return value of requireSession is a YahooSession object (a session), which you will use next to make YQL queries.

To call the YQL Web service, pass the YQL query to the method query from the YahooSession object $session. In the code snippet below, the results of the queries are passed to the variables $user_updates and $user_connections to be parsed for specific data.

Source Code

Data Scraping with YQL

Summary

This example, yql_html_scraper.html, uses YQL to scrape HTML from Yahoo! Finance 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 and 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! Finance. The variable ticker stores the stock symbol of the company given by the user. See Using XPath in YQL Statements for details about the YQL query.

  4. Instantiate a YQL object by passing the YQL query to the constructor.

  5. To initiate the call to the YQL Web service, call the on method from the YQL object.

  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 headline stories for the company represented by {ticker_sym}. 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://finance.yahoo.com/q?s={ticker_sym}" and xpath='//div[@id="yfi_headlines"]/div[2]/ul/li/a'

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 Yahoo! Inc (YHOO) page on Yahoo! Finance.

url="http://finance.yahoo.com/q?s=yhoo"

The XPath statement below extracts the bulleted links from the third div child of the div with the id of "yfi_headlines". Each link (a element) is returned as a row (repeated element in XML).

xpath='//div[@id="yfi_headlines"]/div[2]/ul/li/a'

For more information about XPath, see WC3 XPath Language.

Source Code

Table of Contents