Interspire API and writing your own functions

Check the Interspire Knowledge Base for more information

Once you have created your XML request string, you need to cUrl it to the xml.php file in your shopping cart root (or wherever you’ve moved it).  You do this – according to the Email Marketer API documentation – as follows:

$xml = "";
$xml .= "<xmlrequest>";
$xml .= "<username>apiuser</username>";
$xml .= "<usertoken>blahblahblahauthtoken</usertoken>";
$xml .= "<requesttype>products</requesttype>";
$xml .= "<requestmethod>GetProduct</requestmethod>";
$xml .= "<details>";
$xml .= "   <productId>100</productId>";
$xml .= "</details>";
$xml .= "</xmlrequest>";

$ch = curl_init("http://www.example.com/xml.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
$result = @curl_exec($ch);

var_dump($result);

Simple! Recently, I needed to retrieve something else than the functionality provided by the API. I had implemented a WordPress blog in a sub directory, but still wanted to keep the “Shopping Cart (x items)” in the header. I had tried to create the functionality manually, but the Interspire Shopping Cart code is so intrinsically linked that it was impossible (without copying and chopping up half the whole site).

It turned out to be quite easy (sorry, SIMPLE - not easy) to create the API.  Here’s how:

Create a file called “class.cart.api.php” in %%site_root%%/includes/classes/api/

class.cart.api.php


// this needs to be included (you could obviously copy the required functions yourself, but why bother)
require_once(ISC_BASE_PATH . '/lib/general.php');

class API_CART extends API_BASE {

    private $sessionClass = "";
    private $sessionData = "";
    private $sessionKey = "";

    public function Action_GetTotalItems() {

        // Create a default, so we know if there was an error

        $numItems = -1;

        // We check if a session key was passed into the API - otherwise return an error
        if (empty($this->router->request->details->sessionKey)) {
            $this->BadRequest('The details->productId node is missing');
        }

        // retrieve sessionKey from request XML
        $this->sessionKey = (string)$this->router->request->details->sessionKey;

        // retrieve session
        $this->RetrieveSession();

        // if all has went well, we should have an ISC_QUOTE class in our newly created SESSION
        // using the functions in general.php, we can extract the number of items in the cart/quote
        if (isset($_SESSION['QUOTE'])) {
            $quote = getCustomerQuote();
            $numItems = $quote->getNumItems();
        }

        // return the:
        // number of items (-1 if it went tits up),
        // a bool of whether the session decode was successfull
        // and our sessionKey - for debugging
        return array(
            "numitems" => $numItems,
            "sessionResponse" => $this->sessionData,
            "sessionKey" => $this->sessionKey
        );
    }

    private function RetrieveSession($sessionKey) {
        if (!empty($this->sessionKey)) {
            // this function creates a new ISC_SESSION class and passes in the sessionKey
            // it will then attempt to retrieve and decode the session data from the database
            $this->sessionClass = new ISC_SESSION($this->sessionKey);
            $this->sessionData = $this->sessionClass->LoadSessionByToken($this->sessionKey);
        }
    }
}

Now, you might be wondering where we get our Session Key from… well:

mycallingfile.php

$xml = "";
$xml .= "<xmlrequest>";
$xml .= "<username>apiuser</username>";
$xml .= "<usertoken>blahblahblahauthtoken</usertoken>";
$xml .= "<requesttype>cart</requesttype>";
$xml .= "<requestmethod>GetTotalItems</requestmethod>";
$xml .= "<details>";
$xml .= "   <sessionKey>".$_COOKIE["SHOP_SESSION_TOKEN"]."</sessionKey>";
$xml .= "</details>";
$xml .= "</xmlrequest>"."";

$ch = curl_init("http://www.example.com/xml.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
$result = @curl_exec($ch);

var_dump($result);

The beauty of it is, is the API handler will search for the file “class.” + (the request type) + “.api.php”, and then check that “Action_” + (the request method) exists before executing, so there’s no registration involved, just make sure the file is called “class.cart.api.php”, and it has a function “Action_GetTotalItems” in it.

And that’s it… it took me better part of a day to figure this little lot out, and you get it in 5 minutes – aren’t you lucky!

6 thoughts on “Interspire API and writing your own functions

  1. Hi,

    Thanks for your help… That’s a nice tutorial about using Interspire API that helps me a lot doing some of my scripts.

    But i have one question, is possible to get the full cart? Products, Product Name, Qty, total value, etc?

    Thanks

    Mozack

  2. Hey Mozack… sorry for the late response. I get a lot of spam on these so I don’t tend to look that often.

    Yeah, it should be possible… Once you’ve got access to the QUOTE class on line 30… you can use the getItems ($quote->getItems()) function which will return an array of the items in the cart thus far. I can’t remember off hand what’s inside the array but price, qty should be in there… you can use the $quote->getSubTotal($includingTax) to get the full total etc…

    the class it uses is in /includes/classes/class.quote.php look at the functions you can use to return data.

  3. I am looking to link Interspire Email Marketing and WordPress to match up the databases between the two automatically. Would like to find out if you have done something similar.

    Please advise.

  4. Thanks so much for this! Some of us are still using the self-hosted version of ISC and are left to manage the cart with no more support from Interspire so information like this has been a HUGE help. We just installed wordpress in a directory under our main ISC site as well and would love to have some kind of integration. There use to be a wordpress plugin that supported the self-hosted version of ISC but they only updated it for BigCommerce now and the older version of it doesn’t work 1. with a large database of products and 2. if you use the CustomSlugs mod for SEO URLs (both of which apply to us). Their plugin was just calling the ISC API and grabbing all of the products and dumping them into the options table of wordpress all at once, Yikes!

    Armed with this info, we are hoping to be able to easily link products from our store directly in our wordpress posts without having to copy and paste each link manually.

    With this API created how did you implement it in your blog?

    I would imagine one could also use it to create a wordpress widget…?

  5. Hey Jeffrey

    Sorry for the late reply. I haven’t done anything in the way of a plug in, but I wrote a simple class to insert data into the EMM. I’ll post that soon.

  6. Hi Mila

    Because the wordpress end was quite simple, I didn’t need to create a widget. I just embedded the code directly into the theme (as it was custom made by our designer). Sorry :(

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>