February 6, 201214 yr Okay so I'm using the Google Weather API. Sometimes though, it's SO slow, which makes the site slow. Is there any way to make it the last thing to load? And also, if it takes too long, it's not loaded at all? It also, sometimes, throws this up: : Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /home/kaexec/public_html/wp-content/themes/kaexec/header.php:87 Stack trace: #0 /home/kaexec/public_html/wp-content/themes/kaexec/header.php(87): SimpleXMLElement->__construct('') #1 /home/kaexec/public_html/wp-content/themes/kaexec/header.php(100): getWeather() #2 /home/kaexec/public_html/wp-includes/theme.php(1115): require_once('/home/kaexec/pu...') #3 /home/kaexec/public_html/wp-includes/theme.php(1091): load_template('/home/kaexec/pu...', true) #4 /home/kaexec/public_html/wp-includes/general-template.php(34): locate_template(Array, true) #5 /home/kaexec/public_html/wp-content/themes/kaexec/home.php(16): get_header() #6 /home/kaexec/public_html/wp-includes/template-loader.php(43): include('/home/kaexec/pu...') #7 /home/kaexec/public_html/wp-blog-header.php(16): require_once('/home/kaexec/pu...') #8 /home/kaexec/public_html/index.php(17): require('/home/kaexec/pu...') #9 {main} thrown in /home/kaexec/public_html/wp-content/themes/kaexec/header.php on line 87 This has only happened today. Line 87 is: $xml = new SimplexmlElement($xml_str); Thing is, when you make the API call in browser, it's instant: http://www.google.com/ig/api?weather=bn424nt&hl=en So I don't know what to do... Full code: <?php function getWeather() { $ch = curl_init(); $timeout = 5; // set to zero for no timeout curl_setopt ($ch, CURLOPT_URL, 'http://www.google.com/ig/api?weather=bn424nt&hl=en'); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); $xml_str = curl_exec($ch); curl_close($ch); $xml = new SimplexmlElement($xml_str); $count = 0; foreach($xml->weather as $item) { foreach($item->current_conditions as $new) { echo '<div class="weathericon">'; echo '<img src="http://www.google.com/' . $new->icon['data'] . '"/></div>'; echo '<div id="weather">'; echo round((($new->temp_f['data']* 1)-32) * (5 * 1) / (9 *1),1) . '°C<br>'; echo '<p>Forecast</p>' . $new->wind_condition['data'] . '</div>'; } } } getWeather(); ?> Edited February 6, 201214 yr by brightonmike
February 6, 201214 yr I can think of a couple of ways to speed things up here - you could use jQuery.parseXML() asynchronously instead of holding up the page render while it's done on the server. If it's WordPress, I'd suggest caching to a transient rather than fetching and parsing the remote file every page request.
February 6, 201214 yr Author I can think of a couple of ways to speed things up here - you could use jQuery.parseXML() asynchronously instead of holding up the page render while it's done on the server. If it's WordPress, I'd suggest caching to a transient rather than fetching and parsing the remote file every page request. I'll look up both methods...
February 6, 201214 yr Author Bleh, the API seems to be killing the site altogether http://www.kaexec.com/ Actually, I think it's either my net, or placehold.it Edited February 6, 201214 yr by brightonmike
Create an account or sign in to comment