Web Design Forum: Slow API - Web Design Forum

Jump to content

WDF
WDF Premium Memberships Reseller Hosting
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Slow API Rate Topic: -----

#1 User is online   brightonmike 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 2,996
  • Joined: 27-June 11
  • Reputation: 340
  • Gender:Male
  • Experience:Intermediate
  • Area of Expertise:Web Designer

Posted 06 February 2012 - 03:59 PM

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.co...r=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) . '&deg;C<br>';	
			   echo '<p>Forecast</p>' . $new->wind_condition['data'] . '</div>';
               }
          }
}
getWeather();
?>	

This post has been edited by brightonmike: 06 February 2012 - 03:59 PM

0

#2 User is online   Renaissance-Design 

  • Available for custom WordPress work
  • View blog
  • Group: Moderators
  • Posts: 3,595
  • Joined: 12-August 10
  • Reputation: 559
  • Gender:Male
  • Location:South Wales
  • Experience:Web Guru
  • Area of Expertise:Designer/Coder

Posted 06 February 2012 - 04:10 PM

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

#3 User is online   brightonmike 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 2,996
  • Joined: 27-June 11
  • Reputation: 340
  • Gender:Male
  • Experience:Intermediate
  • Area of Expertise:Web Designer

Posted 06 February 2012 - 04:13 PM

View PostRenaissance-Design, on 06 February 2012 - 04:10 PM, said:

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

#4 User is online   brightonmike 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 2,996
  • Joined: 27-June 11
  • Reputation: 340
  • Gender:Male
  • Experience:Intermediate
  • Area of Expertise:Web Designer

Posted 06 February 2012 - 04:21 PM

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

This post has been edited by brightonmike: 06 February 2012 - 04:24 PM

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users