Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Slow API

Featured Replies

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 by brightonmike

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.

  • 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...

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.