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 8737385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:30:33+00:00 2026-06-13T10:30:33+00:00

I was wondering what can I do to not do multiple calls to API?

  • 0

I was wondering what can I do to not do multiple calls to API? For instance, if I’m trying to use yelp’s api, they have a limit of calls before exceeding (they stop giving you information once you exceed that number).

What can I do? Right now, I have this (from their API PHP example) in every page that I require to get information from their site:

// For example, request business with id 'the-waterboy-sacramento'
//$unsigned_url = "http://api.yelp.com/v2/business/the-waterboy-sacramento";


// For examaple, search for 'tacos' in 'sf'
//$unsigned_url = "http://api.yelp.com/v2/search?term=tacos&location=sf";

// My own code
$unsigned_url = "http://api.yelp.com/v2/search?term=".$term."";
// $term is coming from searching

// Set your keys here
$consumer_key = "some_id";
$consumer_secret = "some_id";
$token = "some_id";
$token_secret = "some_id";

// Token object built using the OAuth library
$token = new OAuthToken($token, $token_secret);

// Consumer object built using the OAuth library
$consumer = new OAuthConsumer($consumer_key, $consumer_secret);

// Yelp uses HMAC SHA1 encoding
$signature_method = new OAuthSignatureMethod_HMAC_SHA1();

// Build OAuth Request using the OAuth PHP library. Uses the consumer and token object created above.
$oauthrequest = OAuthRequest::from_consumer_and_token($consumer, $token, 'GET', $unsigned_url);

// Sign the request
$oauthrequest->sign_request($signature_method, $consumer, $token);

// Get the signed URL
$signed_url = $oauthrequest->to_url();

// Send Yelp API Call
$ch = curl_init($signed_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch); // Yelp response
curl_close($ch);

And this seems excessive to be doing in EVERY page when I want to call a certain business.

  • 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-06-13T10:30:34+00:00Added an answer on June 13, 2026 at 10:30 am

    I don’t use it but, as a general answer, consider caching the data in a database to reduce API calls. It will also improve your site’s performance, especially if the same page that needs the same data from the API loads multiple times.

    HERE’S A FILE BASED GENERAL USE IMPLEMENTATION

    /**
    * Cache friendly function call stub.
    * 
    * @param callback $Callback
    * @param array $Arguments
    * @param integer $Expiration
    * @return mixed
    */
    function CallFunctionAndCacheResult($Callback, array $Arguments, $Expiration = 3600){
        // Validate $Callback
        if(!is_callable($Callback)){
            trigger_error('$Callback must be a callback.', E_USER_WARNING);
            return false;
        }
        // Validate $Arguments (we need some to hash)
        if(empty($Arguments)){
            trigger_error('$Arguments cannot be empty.', E_USER_WARNING);
            return false;
        }
        // Empty $Expiration means live calls
        if(empty($Expiration)){
            return call_user_func_array($Callback, $Arguments);
        }
        // Validate $Expiration (we need some to hash)
        if(!is_numeric($Expiration) or (($Expiration = intval($Expiration)) < 1)){
            trigger_error('$Expiration has to be a positive integer.', E_USER_WARNING);
            return false;
        }
        // Hash the Arguments (Unique Call ID)
        $Hash = md5(serialize($Arguments));
        // Check if Cache file exists
        if(is_file($Cache = dirname(__FILE__)."/{$Hash}.serial")){
            // Test if file expired
            if(($MTime = filemtime($Cache)) >= (time() - $Expiration)){
                // Attempt to load data, assume it's corrupted
                if(($Data = unserialize(file_get_contents($Cache))) !== false){
                    return $Data;
                }
            }
        }
        // Now regenerate the data if you got here
        $Data = call_user_func_array($Callback, $Arguments);
        // Store it, with  LOCK_EX enabled to prevent collisions
        file_put_contents($Cache, serialize($Data), LOCK_EX);
        // Return the data
        return $Data;
    }
    
    // Test case (store current time for 10 seconds, keep refreshing)
    var_dump($Data = CallFunctionAndCacheResult(function(){
        // Return some data to be cached
        return array_merge(func_get_args(), array(
            'cached'    => gmstrftime('%A, %d %B %Y %H:%M:%S'),
        ));
    }, array(
        'Username',
        'Password',
        // ...
    ), 10));
    

    Hope it makes sense. Just test it, figure it out and use a DB engine to achieve the same result.

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

Sidebar

Related Questions

I was wondering: With a tree, the root can have multiple children and no
I was wondering, why AnyVal can not be used in an isInstanceOf check ?
I was wondering whether or not I can extend the Enum type in C#
I was wondering, why can't there be a void data type that is not
I was wondering if it was safe to make GL calls with multiple threads.
I'm not sure what it's called exactly, but I was wondering how you can
What control structures can one use instead of multiple nested IF statements. eg: function
I am wondering how I can break up my index.php homepage to multiple php
Just wondering if any Firebird users can confirm that batched queries is not a
Possible Duplicate: ASP.NET MVC - Can i have multiple names for the same action?

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.