Jump to content

Yahoo Widget


newtoajax

Recommended Posts

I cannot figure out why this weather widget is not working, can someone please take a look at it and see what I did wrong. I have the weather.php file in the same directory. I keep getting this error message: 0 communication failure. Thanks a lot. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html><head> <meta http-equiv="content-type" content="text/html; charset=utf-8"><title>Retrieving a Yahoo! Weather RSS Feed</title> <style type="text/css">/*margin and padding on body element can introduce errors in determining element position and are not recommended; we turn them off as a foundation for YUI CSS treatments. */body {margin:0;padding:0;}</style> <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.9.0/build/fonts/fonts-min.css" /><!-- Dependency --><script src="http://yui.yahooapis.com/2.9.0/build/yahoo/yahoo-min.js"></script> <!-- Used for Custom Events and event listener bindings --><script src="http://yui.yahooapis.com/2.9.0/build/event/event-min.js"></script> <!-- Source file --><!--If you require only basic HTTP transaction support, use theconnection_core.js file.--><script src="http://yui.yahooapis.com/2.9.0/build/connection/connection_core-min.js"></script> <!--Use the full connection.js if you require the following features:- Form serialization.- File Upload using the iframe transport.- Cross-domain(XDR) transactions.--><script src="http://yui.yahooapis.com/2.9.0/build/connection/connection-min.js"></script><!--there is no custom header content for this example--> </head> <body class=" yui-skin-sam"> <h1>Retrieving a Yahoo! Weather RSS Feed</h1> <div class="exampleIntro"><p>This example demonstrates how to use the <a href="http://developer.yahoo.com/yui/connection/">Connection Manager</a> and a PHP proxy — to work around XMLHttpRequest's same-domain policy — to retrieve an XML document from <code>http://xml.weather.yahoo.com/forecastrss</code>.</p> <p>To try out the example, fill in your five-digit US zip code, or Location ID.</p></div> <!--BEGIN SOURCE CODE FOR EXAMPLE =============================== --> <form id="wForm"><fieldset><label>Zip Code or Location ID</label> <input type="text" name="zip" value="94089"><p>Please enter a U.S. Zip Code or a location ID to get the current temperature. The default is Zip Code 94089 for Sunnyvale, California; its location ID is: USCA1116.</p></fieldset><div id="weatherModule"></div><input type="button" value="Get Weather RSS" onClick="getModule()"></form><script>var div = document.getElementById('weatherModule');var oForm = document.getElementById('wForm'); function successHandler(o){//YAHOO.log("Success handler called; handler will parse the retrieved XML and insert into DOM.", "info", "example"); var root = o.responseXML.documentElement;var oTitle = root.getElementsByTagName('description')[0].firstChild.nodeValue;var oDateTime = root.getElementsByTagName('lastBuildDate')[0].firstChild.nodeValue;var descriptionNode = root.getElementsByTagName('description')[1].firstChild.nodeValue; div.innerHTML = "<p>" + oTitle + "</p>" + "<p>" + oDateTime + "</p>" + descriptionNode; //YAHOO.log("Success handler is complete.", "info", "example");} function failureHandler(o){//YAHOO.log("Failure handler called; http status: " + o.status, "info", "example"); div.innerHTML = o.status + " " + o.statusText;} function getModule(){var iZip = oForm.elements['zip'].value;var entryPoint = 'weather.php';var queryString = encodeURI('?p=' + iZip);var sUrl = entryPoint + queryString; //YAHOO.log("Submitting request; zip code: " + iZip, "info", "example"); var request = YAHOO.util.Connect.asyncRequest('GET', sUrl, { success:successHandler, failure:failureHandler });}//YAHOO.log("When you retrieve weather RSS data, relevant steps in the process will be reported here in the logger.", "info", "example"); </script> <!--END SOURCE CODE FOR EXAMPLE =============================== --> </body></html><!-- presentbright.corp.yahoo.com uncompressed/chunked Thu Feb 19 10:53:10 PST 2009 -->

Link to comment
Share on other sites

is that just example code, or your actual own implementation? Do you actually have a weather.php for making the actual request? You understand that you need to use PHP (like cURL or HTTP) as proxy to get around AJAX's same origin/domain policy, right?

Link to comment
Share on other sites

Here is the php file. thnx //Within a PHP block: // Since the result is an XML document, the Content-type // header must be set to "text/xml" for the data to be // treated as XML and to populate responseXML. header("Content-Type:text/xml"); // $url is the resource path of the Y! Weather RSS // with the appended querystring of zip code/location id. $url = 'http://xml.weather.yahoo.com/forecastrss?'.getenv('QUERY_STRING'); // This function initializes CURL, sets the necessary CURL // options, executes the request and returns the results. function getResource($url){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($ch); curl_close($ch); return $result; } // Call getResource to make the request. $feed = getResource($url); // Return the results. echo $feed;

Link to comment
Share on other sites

  • 2 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...