Web lists-archives.com

[PHP] Block until API result cached, then re-instantiate API class

Hello fellow coders.

What I’m trying to do is make a call to an API, cache the results and potentially it again. The Mindbody API only allows one concurrent call.

Using a Class to make a call to a SOAP API (MindbodyOnline) which can only have a single connection per instance (API methods will not be available upon additional concurrent calls), but I want to be able to have multiple instances of various resulting data.

I am using wordpress _transients to store the result in a database, and can actually get multiple "instances" by:

  • call the function once (with a shortcode)
  • load the page (creating a cached version)
  • call function a second time (second shortcode)
  • reload the page

Now there are two separate cached versions of the API result. But this is not a working solution for many reasons.

From my limited understanding of how php works, all of the "page code" is interpreted and compiled as C code, then run at the same time. Is this correct?

Is there some way to make a class with a method that will:

  1. Instantiate the class which calls the API
  2. Cache the result

For each time it is called within a page?

Maybe something along these lines:

  if ( false === ( $staff = get_transient( $mz_staff_cache ) ) )
    $mb = instantiate_mbo_API(); // function instantiates API class

    /* Some check here to see if we need to call API again
    * and re-instantiate somehow if we do?

    $staff = $mb->GetStaff(); // This is an API class method
    set_transient($mz_staff_cache, $staff, 60 * 60 * 24);

  // Do stuff with $staff
I’m thinking running multiple API queries with AJAX might be an answer, but sending information back and forth from the browser multiple times doesn’t seem quite right.
It’s a wordpress plugin so that limits the possibilities in ways - like calling a sub-process to C or python or something wouldn’t work.
Any thoughts?

Michael Kilmer
Media Zen Organic Outreach 
Music, Theater, Multimedia and Web Development

Attachment: signature.asc
Description: Message signed with OpenPGP using GPGMail