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.
Setting Up this Example
To set up and run yql_query_with_js.html:
- Copy
yql_query_with_js.htmlto your Web server. - From My Projects, create a new Open Application.
- In the Application Editor, specify the URL to
yql_query_with_js.htmlin the "Application URL" field. - Click "Preview" to see the canvas view. Your application will have a text field for entering a location. Enter "SFO" and click
"Make Query" to see results similar to those in the figure below.

Source Code
YQL Calls With PHP
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 PHP SDK is ideal because the PHP SDK will handle your OAuth authorization, which is required to access the Social Directory APIs. YQL, in turn, extends the functionality of the PHP 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.
Prerequisites
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:
Setting Up this Example
To set up and run the WordPress Open Application:
- From My Projects, create a new Open Application and copy the code below into the Small View Default Content window:
- Click Save and then Preview. Your Small view should look similar to that in the figure below:

- Copy
yql_insert_wordpress.phpto your Web server. - Make sure the path to the SDK is correct in your script. From the Permissions tab, copy the Consumer Key and Shared Secret
into the
definefunction calls as seen below: - In the Application URL text field, enter the URL to the
yql_insert_wordpress.phpand then click Save. - From the Preview window for the Small View, enter the username, password, and the URL to your WordPress blog site (either self-hosted site or hosted by WordPress).
- Now enter a title and some text for your post and click "Publish". You should see a message and the blog post below the form
similar to the figure below:

- Go to your WordPress blog site to confirm that your post was successful. If you are having trouble running the WordPress Open Application, see Troubleshooting Your Application.
- Optional Steps
Try pushing the WordPress Open Application live. From the Status bar, first click Push Live, which will turn the LIVE button green. Now click LIVE.
- You will be redirected to a page to add your application to Yahoo!. Click Add and then from the pop-up dialog box, click 'I
agree' as seen here:

- After you have successfully added the application, click "Add Shortcut to My Yahoo!". You'll be taken to My Yahoo! and asked
if you want to keep the application as seen in the figure below. Click Keep It.

- From the My Yahoo! page, you can now post to your WordPress blog and share your application with others.
Source Code
Small View Code
WordPress Open Application
Getting Updates with YQL
Summary
This code example shows you how to use YQL with the PHP SDK for YSP to get user updates and the updates for the user's connections. The PHP 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.
Setting Up this Example
To set up and run yql_updates.php:
- Copy
yql_updates.phpto your Web server. - From My Projects, create a new Open Application.
- From the Application Editor, copy the Consumer Key and the Shared Secret and then paste the keys into
yql_updates.php. - In the Application Editor, specify the URL to
yql_updates.phpin the "Application URL" field. - Click "Preview" to see the canvas view. You should see your updates and the updates of your connections as seen in the figure
below.


