Bright Hub
 
Matthew Casperson's Hubfolio

Yahoo Query Language (YQL) and PHP

Article by Matthew Casperson (4,883 pts )
Published on Nov 1, 2009

This free tutorial shows you how to use the YQL web service from PHP.

Introduction

DOWNLOAD THE SOURCE CODE

The Yahoo Query Language, or YQL, is a web service that exposes a number of Yahoo and 3rd party services through a consistent SQL like query syntax. With this one services you can consume and mashup hundreds of services with the one API. It saves you the time you would have spent learning separate APIs, and reduces the amount of code you have to write.

YQL returns data in either JSON or XML. Working with JSON is relatively painless in PHP with the native json_decode and json_encode functions. In this tutorial we will build a simple weather page in PHP using data returned from YQL.

PHP and YQL Screenshot

Step 1 - The YQL URL

Access to YQL is done through a REST interface, so the first thing we need to do is generate the URL that PHP will call.

$yqlUrl = "http://query.yahooapis.com/v1/public/yql";

$query = "select * from weather.forecast where location=90210";

$queryUrl =

$yqlUrl .

"?q=" . urlencode($query) .

"&format=json" .

"&env=" . urlencode("store://datatables.org/alltableswithkeys");

This URL is built by using the YQL Console. Select the weather table from the Data Tables list in the bottom right hand corner, and click on the weather.forecast link.

PHP and YQL Screenshot

The YQL Console will then create the URL for the REST query. You just need to remove the callback URL parameter.

PHP and YQL Screenshot

As you can see the code above simply builds up the REST query from the YQL Console in a few steps, to make it easy to see how the URL is constructed.

Step 2 - Call the YQL service

With the URL in hand we then need to call the YQL service.

PHP offers more than one way to contact web services. You can use the file_get_contents function, but I prefer to use CURL. CURL is a powerful library that allows you to interact with web servers via a number of protocols. The best thing about CURL is that there is a command line version that lets you easily test your code without having to debug the PHP directly.

$session = curl_init($queryUrl);

curl_setopt($session, CURLOPT_HEADER, false);

curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

$json = curl_exec($session);

curl_close($session);

Here we:

  • initialise CURL,
  • set CURL not to return the HTTP headers by setting CURLOPT_HEADER to false
  • set CURL to return the contents of the HTTP request by setting CURLOPT_RETURNTRANSFER to true
  • execute the request, saving the result to the $json variable
  • clean up CURL

This is a fairly standard process when using CURL to access REST web service interfaces.

Step 3 - Convert JSON to PHP

PHP comes with a built in function called json_decode which will take the JSON returned by YQL and convert it into a PHP object.

$yqlObject = json_decode($json);

Step 4 - Display the results

Inside the webpage we can now use the $yqlObject variable to display the results of the YQL query.

<?php

print $yqlObject->{'query'}->{'results'}->{'channel'}->{'item'}->{'description'};

?>

Here we display the weather description, which is a HTML formatted field. To see what fields are available, use the Tree View in the YQL Console.

PHP and YQL Screenshot

Conclusion

YQL saves you time by exposing hundreds of web services through one easy to learn interface. If you are creating a mashup, or just want to reference a single external web service, YQL and the tools like the YQL Console are a great resource, and well worth using.

Back to the Tutorial Index

Related Article

flexUsing Yahoo Query Language (YQL) from Flex

This free tutorial steps you through the process of accessing the YQL service from Flex.

Search More About:

 
Follow Matthew Casperson
Receive weekly updates from Matthew Casperson
 
Bright Hub - Science & Technology Articles, Buyer's Guides, How-To Tips and Software Reviews
About Bright Hub | Contact Us | Advertise with Us | Become a Writer | RSS | Site Map | Terms of Use | Privacy Policy | Copyright Policy
©2009 Bright Hub Inc. All rights reserved. Page copy protected against web site content infringement by Copyscape