This example demonstrates how to use the Connection Manager and a PHP proxy — to work around XMLHttpRequest's same-domain policy — to retrieve an XML document from http://xml.weather.yahoo.com/forecastrss
.
To try out the example, fill in your five-digit US zip code, or Location ID.
Load the Yahoo Global Object and Connection Manager source files:
1 | <script src="yahoo.js"> |
2 | <script src="connection.js"> |
view plain | print | ? |
Yahoo! Weather RSS will return an XML document if the transaction is successful. The following callback object with success
and failure
handlers is used to process the response.
1 | // This is the response display container |
2 | var div = document.getElementById('weatherModule'); |
3 | // This is a reference to the HTML form. |
4 | var oForm = document.getElementById('wForm'); |
5 | |
6 | /* |
7 | * This method will traverse the response XML document and build a |
8 | * simple HTML module comprised of data from the following tags: |
9 | * |
10 | * Data in the first <title> tag in the document. |
11 | * Data in the first <lastBuildDate> tag in the document. |
12 | * HTML from the second <description> tag in the document. |
13 | * |
14 | */ |
15 | function successHandler(o){ |
16 | var root = o.responseXML.documentElement; |
17 | var oTitle = root.getElementsByTagName('description')[0].firstChild.nodeValue; |
18 | var oDateTime = root.getElementsByTagName('lastBuildDate')[0].firstChild.nodeValue; |
19 | var descriptionNode = root.getElementsByTagName('description')[1].firstChild.nodeValue; |
20 | |
21 | // Format and display results in the response container. |
22 | div.innerHTML = "<p>" + oTitle + "</p>" + "<p>" + oDateTime + "</p>" + descriptionNode; |
23 | } |
24 | |
25 | /* |
26 | * |
27 | * This is a simple failure handler that will display |
28 | * the HTTP status code and status message if the resource |
29 | * returns a non-2xx code. |
30 | * |
31 | */ |
32 | function failureHandler(o){ |
33 | div.innerHTML = o.status + " " + o.statusText; |
34 | } |
view plain | print | ? |
The Yahoo! Weather RSS feed requires a simple HTTP GET request with a base URL and a querystring containing the required information as a name-value pair. In this example, we will use the following parameter:
p
— location as U.S. Zip Code or Location IDThe following are some example location IDs (do not include the city name):
For more details on the Yahoo! Weather RSS feed and other location IDs, please visit http://developer.yahoo.com/weather/index.html.
Function getModule
retrieves the input values for location and creates a querystring:
1 | function getModule(){ |
2 | |
3 | // retrieve the field values for the zip code input |
4 | var sLocation = oForm.elements['zip'].value; |
5 | |
6 | // entryPoint is the base URL |
7 | var entryPoint = 'php/weather.php'; |
8 | |
9 | // queryString is the key-value pair of the zip code |
10 | // or location id. |
11 | var queryString = encodeURI('?p=' + sLocation); |
12 | var sUrl = entryPoint + queryString; |
13 | |
14 | // Initiate the HTTP GET request. |
15 | var request = YAHOO.util.Connect.asyncRequest('GET', sUrl, { success:successHandler, failure:failureHandler }); |
16 | } |
view plain | print | ? |
Once weather.php
receives the querystring, it will construct and send an HTTP GET using CURL to retrieve the results from the Yahoo! Weather RSS feed. This allows the transaction to succeed while working around XMLHttpRequest's strict security policy.
1 | //Within a PHP block: |
2 | |
3 | // Since the result is an XML document, the Content-type |
4 | // header must be set to "text/xml" for the data to be |
5 | // treated as XML and to populate responseXML. |
6 | header("Content-Type:text/xml"); |
7 | |
8 | // $url is the resource path of the Y! Weather RSS |
9 | // with the appended querystring of zip code/location id. |
10 | $url = 'http://xml.weather.yahoo.com/forecastrss?'.getenv('QUERY_STRING'); |
11 | |
12 | // This function initializes CURL, sets the necessary CURL |
13 | // options, executes the request and returns the results. |
14 | function getResource($url){ |
15 | $ch = curl_init(); |
16 | curl_setopt($ch, CURLOPT_URL, $url); |
17 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
18 | $result = curl_exec($ch); |
19 | curl_close($ch); |
20 | |
21 | return $result; |
22 | } |
23 | |
24 | // Call getResource to make the request. |
25 | $feed = getResource($url); |
26 | |
27 | // Return the results. |
28 | echo $feed; |
view plain | print | ? |
You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.
INFO 1043ms (+345) 12:04:32 AM:
LogReader instance0
LogReader initialized
INFO 698ms (+0) 12:04:31 AM:
Get
Appending node: ../../../2.x/build/event-mouseenter/event-mouseenter-min.js
INFO 698ms (+0) 12:04:31 AM:
Get
attempting to load ../../../2.x/build/event-mouseenter/event-mouseenter-min.js
INFO 698ms (+173) 12:04:31 AM:
Get
_next: q0, loaded: undefined
INFO 525ms (+525) 12:04:31 AM:
example
When you retrieve weather RSS data, relevant steps in the process will be reported here in the logger.
INFO 0ms (+0) 12:04:31 AM:
global
Logger initialized
Note: You are viewing this example in debug mode with logging enabled. This can significantly slow performance.
Copyright © 2011 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Copyright Policy - Job Openings