Magento , PHP and Visual Studio .NET - Using Magento Soap API v1 & v2 in a slightly different way than the rest...



If you are having problems trying to use Magento's API in Visual Studio .NET - Then you are not alone..
For over two years i have been maintaining a .NET Application for a companies Website. Since Version 1.2.x till version 1.4.1.1  (Version 1.6.x is released on publishing this post) I have been using the profiles to export all items into some loosely typed xml document and process the items offline. At the time of 1.2.x i wrote my own parser that works till today, just throws wobbles evey now and again if some uncaught error occurs.

Magento has always had a web API that was easily accessible using PHP. Java/.NET could not connect to this enpoint because it was loosely typed and does not conform to some standards. So somewhere they released the API2 that uses strongly typed members and that can be easily added to .NET/ Java Applications.

If you look hard enough you will find 1 or 2 guides on how to connect VS.NET to this SOAP service.. but you will still need to write your own wrapper class to make life Object Orientated and reusable.. Fine and dandy i did all the hard work and works well.. But this post is not about that..


..this post is about how the API v2 lacks the ability to send and array of requests to the Magento API. In v1 there exists a call "multiCall" that if you wish you can pass 500,1000 arrays and the API processes the request and returns some data in PHP Array format. In API v2 ... Where and how the &$*% do you do that?? Nope.. sorry you cant! I went as far as to build threaded classes and do 3 requests a seconds (1 request for 1 items extended information) That roughly translates to 1350 items in 25 minutes if you are having a good day! WTF i kept on asking myself.

For months i tried understanding what the heck is going on. It turns out only the API v1 supports this and forget about it in v2. End of post.....





It would be the end, if i were not a determined and stubborn person. And here is how to overcome your problem. With this solution i can now download 100products a seconds (from a cheap as dirt Atom server somewhere in the basements of Kuwait City) So that's 1350products in roughly 45...... SECONDS!!


The trick is to make your own proxy script in the root of the Magento install. I did not find this information anywhere- it just evolved to this and solved all my problems.

Save a file called  getitems.php than contain this... on the root of your magento dir.




$id_start = $_GET['start'];
$id_end = $_GET['end'];

// Magento login information 
$mage_url = 'http://www.YOURDOMAIN.co.uk/api/?wsdl=1'; 
$mage_user = 'API-USER'; 
$mage_api_key = 'API-USER-PASSWORD'; 

// Initialize the SOAP client 
$soap = new SoapClient( $mage_url ); 

// Login to Magento 
$session_id = $soap->login( $mage_user, $mage_api_key );
    

    $calls = array(); 
    for ($id_start; $id_start <= $id_end; $id_start++)
        {
        array_push($calls, array( 'catalog_product.info', $id_start ));
        }

    //And this is what its all about.. the multicall function
    $results = $soap->multiCall( $session_id, $calls ); 
    
     
    //will print the array.
    //print_r($results);
        
    //We want json data instead
    echo json_encode($results);

?>




In Visual Studio .NET VB -- Yea VB! The only VB example of Magento on the entire web..


Function getHTTPStream() As String
        Dim myh As HttpWebRequest = _
        HttpWebRequest.Create("http://www.YOURSITE.co.uk/prod.php?start=20&end=80")
        myh.Timeout = 30000
        myh.UserAgent = "Test"
        Dim myR As HttpWebResponse = myh.GetResponse()
        Dim myEnc As Encoding = Encoding.GetEncoding(1252)
        Dim mySr As StreamReader = New StreamReader(myR.GetResponseStream(), myEnc)

        Return mySr.ReadToEnd()
    End Function
    




How it works. (That code needs tweaking obviously- i have not got time to prettify this stuff)


Using the API v2 you can request the entire list of items, but they come back in a simple form. Using that list however you get the product numbers.


You call the proxy file getitems.php with 2 parameters (  start and end ) This is the range of the products you want.. its simple as it can be. The PHP proxy gets an ARRAY then returns a JSON-Encoded string as a response. Then VS.NET reads this text and returns it to you in a string format. How you handle the JSON from there is your own solution. No cross domain problems and not endpoint bullshit involved.


For my purposes i don't even need the API v2 any more as i will call my PHP proxy like this


http://www.my site.co.uk/getitems.php?start=1&end1350


45 Seconds later i have ALL my items (obviously i know item 1350 is the last one added)


That is the whole philosophy behind it. The only reason i could not understand what the hell is going on is because the Magento WIKI document is not worth a pot to pee in. Just some scribbles, notes and some links going to dead pages...



Comments