Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6918473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:51:49+00:00 2026-05-27T09:51:49+00:00

I was playing around with the Google Complete API looking for a quick way

  • 0

I was playing around with the Google Complete API looking for a quick way to get hold of the top 26 most searched terms for various question prefixes – one for each letter of the alphabet.

I wouldn’t count myself a programmer but it seemed like a fun task!

My script works fine locally but it takes too long on my shared server and times out after 30 seconds – and as it’s shared I can’t access the php.ini to lengthen the max execution time.

It made me wonder if there was a more efficient way of making the requests to the API, here is my code:

<?php

$prep = $_POST['question'];

for($i=0;$i<26;$i++){

    $letters = range('a','z');
    $letter = $letters[$i];
    $term = $prep . $letter;

    if(!$xml=simplexml_load_file('http://google.com/complete/search?output=toolbar&q=' . $term)){
        trigger_error('Error reading XML file',E_USER_ERROR);
    }
    do{
        $count = 1;
        $result = ucfirst($xml->CompleteSuggestion->suggestion->attributes()->data);
        $queries = number_format((int)$xml->CompleteSuggestion->num_queries->attributes()->int);                
        echo '<p><span>' . ucfirst($letter) . ':</span> ' . $result . '?</p>';
        echo '<p class="queries">Number of queries: ' . $queries . '</p><br />';

    } while ($count < 0);
}

?>

I also wrote a few lines that fed the question in to the Yahoo Answers API, which worked pretty well although it made the results take even longer and I couldn’t exact match on the search term through the API so I got a few odd answers back!

Basically, is the above code the most efficient way of calling an API multiple times?

Thanks,

Rich

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:51:50+00:00Added an answer on May 27, 2026 at 9:51 am

    You should using user perspective to re-look into this issue, ask yourself,

    Will you like to wait 30 seconds for a web page to load?
    Obviously you dun want

    How can I make the page load faster?
    You are depending on an external resource (google api)
    and not just calling once, but 26 times asynchronously

    So, if you change the above synchronously,
    the total time is reduced form 26 to 1 (with the expenses of network bandwidth)

    Take a look at http://php.net/manual/en/function.curl-multi-exec.php,
    here is first step of optimization

    If you get the above done,
    your time spent on external resource could reduce up to 95%

    Will this good enough ?
    Obviously not yet

    Any call to external resource is not reliable, even is google
    if the network down, DNS not resolvable, your page is going down too

    How to prevent that ?
    You need cache, basically the logic is :-

    • search for existing cache, if found, return from cache
    • if not, query google api synchronously (from a to z)
    • store the result into cache
    • return the result

    However, on-demand process is still not ideal (first user issue the request have to wait longest),
    if you know the permutation of user input (hopefully not that big),
    you can use a scheduler (cronjob) to periodically pull result from google api,
    and store the result locally

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi all I was playing around with the Google Maps Directions API and in
I'm playing around with Google Buzz API from Python, During the OAuth process when
I've been playing around with the google maps api through javascript (I'm also new
I'm playing around with the iosched app from Google I/O 2011. Looking at the
I am playing around with Google's gdata API in python. More specificalyl, I am
I am playing around with the Google API Map Activity. Recently Google Maps for
I am playing around with the Google Maps API V3. I want to create
I am playing around with Google Maps for the first time. And I am
I am playing around with google app engine and I'm thinking about starting an
I'm playing around with Google Analytics goals / funnels . I would like to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.