T-JAM and the Tesco Grocery API

Finally found something I can talk about openly.

Having spent so long working on commercially sensitive applications, which is now in the open.

Anyway, apart from helping the Yahoo! OpenHack day, I also signed up to Tesco’s T-JAM event which certainly looks like it could be interesting.

Tesco’s API is a SOAP API, written in and for C# primarily, so it seems that some people had some issues getting started in PHP.

There is a revised API in the pipeline and also the addition of a REST API as well.

But here are some handy hints for getting started:

First off, PHP 5+ has a SOAP client (and server) built in, so here’s a quick script that generates a valid login to the TESCO dev API.

<?php
define('SOAPSERVER', 'http://www.lansley.com/TescoAPI/TescoAPI.svc?wsdl');
define('DEV_KEY', 'your dev key');
define('APP_KEY', 'your app key');

$user  = 'email address of test user';
$pass  = 'password of test user';
$tesco = new SoapClient(SOAPSERVER);

$params = array(
                'email'          => $user,
                'password'       => $pass,
                'developerKey'   => DEV_KEY,
                'applicationKey' => APP_KEY
              );
$sess = $tesco->__soapCall('Login', array('parameters' => $params));
print_r($sess);

Which generates the following output:

stdClass Object
(
    [LoginResult] => stdClass Object
        (
            [StatusCode] => 0
            [StatusInfo] => 
        )

    [session] => stdClass Object
        (
            [ApplicationKey] => your app key
            [BranchNumber] => 2055
            [Cookies] => stdClass Object
                (
                    [string] => Array
                        (
                            [0] => sessionTest=
                            [1] => v=WggAACp8E%3d%3d
                            [2] => CustomerId=9xxx9
                            [3] => CID=93
                            [4] => BTCCMS=h1qzwp%c%3d
                            [5] => UIMode=00000
                            [6] => PS=CSC=D
                            [7] => SSVars=CustomerName=Bob&basketTotal=&basketCount=&slotStart=&slotEnd=&addrName=
                            [8] => u=oQgttVAeDK%2bg%3d%3d
                            [9] => t=dgcAA28XbJWE%3d
                        )

                )

            [CustomerName] => Bob
            [DeveloperKey] => your dev key
        )

)

I’ve monkeyed around with the values in some of the keys (as these are my login credentials) but you can see the general structure. The session object needs to be stored and used in subsequent requests.

To interrogate the API and view the functions and types available, there are some PHP functions to help:

<?php
define('SOAPSERVER', 'http://www.lansley.com/TescoAPI/TescoAPI.svc?wsdl');
define('DEV_KEY', 'your dev key');
define('APP_KEY', 'your app key');

$tesco = new SoapClient(SOAPSERVER);

print_r($tesco->__getFunctions());

Which shows the following SOAP methods available:

Array
(
    [0] => LoginResponse Login(Login $parameters)
    [1] => LogEventResponse LogEvent(LogEvent $parameters)
    [2] => ProductSearchResponse ProductSearch(ProductSearch $parameters)
    [3] => ListProductOffersResponse ListProductOffers(ListProductOffers $parameters)
    [4] => ListProductCategoriesResponse ListProductCategories(ListProductCategories $parameters)
    [5] => ListProductsByCategoryResponse ListProductsByCategory(ListProductsByCategory $parameters)
    [6] => SetProductRatingResponse SetProductRating(SetProductRating $parameters)
    [7] => GetProductRatingResponse GetProductRating(GetProductRating $parameters)
    [8] => GetMultipleProductRatingsResponse GetMultipleProductRatings(GetMultipleProductRatings $parameters)
    [9] => ListBasketItemsResponse ListBasketItems(ListBasketItems $parameters)
    [10] => AddItemToBasketResponse AddItemToBasket(AddItemToBasket $parameters)
    [11] => ChangeItemQuantityResponse ChangeItemQuantity(ChangeItemQuantity $parameters)
    [12] => DeleteItemFromBasketResponse DeleteItemFromBasket(DeleteItemFromBasket $parameters)
)

And:

<?php
define('SOAPSERVER', 'http://www.lansley.com/TescoAPI/TescoAPI.svc?wsdl');
define('DEV_KEY', 'your dev key');
define('APP_KEY', 'your app key');

$tesco = new SoapClient(SOAPSERVER);

print_r($tesco->__getTypes());

Which shows the following types available:

Array
(
    [0] => struct ArrayOfstring {
 string string;
}
    [1] => struct ResponseStatus {
 int StatusCode;
 string StatusInfo;
}
    [2] => struct Session {
 string ApplicationKey;
 string BranchNumber;
 ArrayOfstring Cookies;
 string CustomerName;
 string DeveloperKey;
}
    [3] => struct ArrayOfProduct {
 Product Product;
}
    [4] => struct Product {
 string Id;
 string ImagePath;
 string Name;
 string OfferPromotion;
 string OfferValidity;
 double Price;
 int Quantity;
 float Rating;
}
    [5] => struct ArrayOfCategory {
 Category Category;
}
    [6] => struct Category {
 ArrayOfCategory Children;
 string Href;
 string Id;
 string Name;
 string RawHtml;
}
    [7] => struct ProductRating {
 string Id;
 int MyRating;
 float Rating;
 int Rating1Count;
 int Rating2Count;
 int Rating3Count;
 int Rating4Count;
 int Rating5Count;
}
    [8] => struct Login {
 string email;
 string password;
 string developerKey;
 string applicationKey;
}
    [9] => struct LoginResponse {
 ResponseStatus LoginResult;
 Session session;
}
    [10] => struct LogEvent {
 Session session;
 string eventText;
}
    [11] => struct LogEventResponse {
 ResponseStatus LogEventResult;
}
    [12] => int char
    [13] => duration duration
    [14] => string guid
    [15] => struct ProductSearch {
 Session session;
 string searchString;
 boolean getRatings;
}
    [16] => struct ProductSearchResponse {
 ResponseStatus ProductSearchResult;
 ArrayOfProduct products;
}
    [17] => struct ListProductOffers {
 Session session;
 boolean getRatings;
}
    [18] => struct ListProductOffersResponse {
 ResponseStatus ListProductOffersResult;
 ArrayOfProduct offers;
}
    [19] => struct ListProductCategories {
 Session session;
}
    [20] => struct ListProductCategoriesResponse {
 ResponseStatus ListProductCategoriesResult;
 ArrayOfCategory categories;
}
    [21] => struct ListProductsByCategory {
 Session session;
 Category shelf;
 boolean getRatings;
}
    [22] => struct ListProductsByCategoryResponse {
 ResponseStatus ListProductsByCategoryResult;
 ArrayOfProduct products;
}
    [23] => struct SetProductRating {
 Session session;
 Product product;
}
    [24] => struct SetProductRatingResponse {
 ResponseStatus SetProductRatingResult;
}
    [25] => struct GetProductRating {
 Session session;
 Product thisproduct;
}
    [26] => struct GetProductRatingResponse {
 ResponseStatus GetProductRatingResult;
 ProductRating productrating;
}
    [27] => struct GetMultipleProductRatings {
 Session session;
 string spaceDelimitedProductIDs;
}
    [28] => struct GetMultipleProductRatingsResponse {
 ResponseStatus GetMultipleProductRatingsResult;
 string productRatingsInfo;
}
    [29] => struct ListBasketItems {
 Session session;
}
    [30] => struct ListBasketItemsResponse {
 ResponseStatus ListBasketItemsResult;
 ArrayOfProduct products;
}
    [31] => struct AddItemToBasket {
 Session session;
 Product item;
}
    [32] => struct AddItemToBasketResponse {
 ResponseStatus AddItemToBasketResult;
}
    [33] => struct ChangeItemQuantity {
 Session session;
 Product item;
}
    [34] => struct ChangeItemQuantityResponse {
 ResponseStatus ChangeItemQuantityResult;
}
    [35] => struct DeleteItemFromBasket {
 Session session;
 Product item;
}
    [36] => struct DeleteItemFromBasketResponse {
 ResponseStatus DeleteItemFromBasketResult;
}
)

Hope this helps, not just with the Tesco Grocery API, but in general debugging SOAP calls from within PHP.

This entry was posted in Development, PHP and tagged , , , , , . Bookmark the permalink.

1 Response to T-JAM and the Tesco Grocery API

  1. Jay says:

    Hi ,

    I am using tesco soap api. but it is not working. it gives me error

    Fatal error: Uncaught SoapFault exception: [WSDL] SOAP-ERROR: Parsing WSDL: Couldn’t load from ‘http://soapapi.decibel.net/DecibelService.svc?wsdl’ : failed to load external entity “http://soapapi.decibel.net/DecibelService.svc?wsdl” in /var/www/tesco/index.php:3 Stack trace: #0 /var/www/tesco/index.php(3): SoapClient->SoapClient(‘http://soapapi….’, Array) #1 {main} thrown in /var/www/tesco/index.php on line 3

    please reply me ASAP

    Thanks,
    Jay

Leave a Reply to Jay Cancel reply

Your e-mail address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.