blog site of branko ajzele, senior developer / project manager

Consuming web service via SOAP in Magento

Consuming Web service via SOAP in Magento is a breeze. There are few steps for you to take in order to get access to SOAP web service. For those of you new to web services and SOAP, let me elaborate few things. SOAP is a protocol for exchanging XML-based messages over computer networks, normally using HTTP/HTTPS. Web services is basically just a set of Web APIs that can be accessed over a network, such as the Internet, and executed on a remote system. Magento Core API supports both SOAP and XML RPC protocols. In this example I will show you how to consume Web service using SOAP protocol.

Create a Web Service Role

Go to your Magento Admin > System > Web Services > Roles

  • Create a role
  • Assign resources to role
Create a web service role

Create a web service role

Create a web service role _screen2

Create a web service role _screen2

Create a web service role _screen3

Create a web service role _screen3

Create a Web Service User

Go to your Magento Admin > System > Web Services > Users

  • Create a user
  • Assign a role to user
Create a web service user

Create a web service user

Create a web service user _screen2

Create a web service user _screen2

Assign user role

Assign user role

And finally the real magic off connecting and retrieving the data from SOAP service. Here is the sample code.

$client = new SoapClient(‘http://somesite.domain/shop/index.php/api/?wsdl’);

$session = $client->login(’sampleuser’, ‘123456′);
$result= $client->call($session, ‘customer.list‘, array());

echo ‘<pre>’;
print_r($result);
echo ‘<pre>’;

One thing to keep in mind here. Notice the /shop part of the URL string? It assumes you have your Magento installed inside the /shop sub folder. Feel free to omit that or change it accordingly if that is not where you installed your Magento.

The tricky part here is the customer.list parameter passed to call() function. In this case our call() function receives three parameters, the second is customer.list. If you are like me, the mere mortal then you might ask your self, where does that parameter come from.

List of parameters for Web services is available from the Magento Core API. If you open the Magento Core API page and go to

Core API Calls > Customer API > Methods

then you will see the customer.list among the list of all the available methods. Basically the parameter we passed to $client->call(arg1, ‘customer.list‘, arg3) was the method name.

I used the above code from WordPress template file and it worked like a charm. I was able to get the customer list in a WordPress template file that I latter displayed on some page like a phone book.

Browsing trough Magento Core API you can see what kind of useful information you can retrieve from Web Service.

If you find yourself in a need of retrieving some other kind of information that is missing from Core API you are free to write your own API (that is if you are up to the task).

Hope someone finds this useful.

  • This is the only damn example of using magento web services user and user roles I can find on the web. I'm trying to utilize this in order to have customers create/add simple products with an image into magento product catalog without being able to see the database. Any ideas? I'm sooo lost....
  • aland
    Thanks for the article, it is useful indeed. What I would love is to have setter methods available, especially for Attributes and Attribute Sets. Wishful thinking I guess.

    Has anyone managed to extend the api with these methods?
  • that was very nice and easy explanation.
    you saved my time. thanks.
  • Another excellent topic on extending Magento. I am quite familiar with Magento and I have been toying with Flex Builder 3 for a short time. I am excited about the prospects of Magento + Flex integrations.

    Since I am new to Flex, my question is... After importing the Magento WSDL, how do I use it in Flex? I guess I am asking for a tutorial from the beginning stages of Flex and Magento integration. Any advice or suggesstions would be appreciated.
  • Hello

    i am a flex programmer and very new to Magento. but i am doing the same with Flex but the little difference is i am using the AIR container to run application( not AIR API.)

    i am runing most of the things accurate with magento but when i have put lot of data at magento then Flex magento communication gives me error #2032 streaming error.

    so in short i get a solution to remove that error. i will use filters and i make the following code for that.

    var arr:Array = new Array();
    var arrDate:Array = new Array();

    arrDate['from'] = ‘2009-04-03 00:00:00′;
    arrDate['to'] = ‘2009-04-05 00:00:00′;
    var tempArr:Array = new Array(arrDate)
    arr['created_at'] = tempArr;

    var mainArray:Array = new Array(arr);
    _objWebService.call(sessionID, “sales_order.list”, mainArray);

    but its again giving me the same error so could you help me to make filter for magento or is there anyotehr problem by which it gives me the same error..

    please help me..
  • wolfgang
    Hi Branko. I love your advise and worked through it - but I can't get running TypoGento at all. Each call results in the error "Warning: SoapClient::SoapClient(http://www.intergrafik.com/magento/index.php/api/?wsdl) [soapclient.soapclient]: failed to open stream: HTTP request failed! HTTP/1.1 503 Service Unavailable in /kunden/206015_8051/webseiten/test.php on line 9" from where the $client = newSoapClient is built. I appreciate any help I could get. Thanks, wolfgang.
  • Richard Max
    Great article. Congrats! V charitable - well done!

    V keen to network with fellow flash gurus about the perfect ecommerce interface (magento powered).

    Nothing inspires me yet re shop interfaces online - I am currently Technical Director at a top eCommerce firm doing v v v average fashion sites that are making a killing (some = £30 million + a year).

    I am convinced that if the designs are more intuitive and engaing, and behaviourale based ecommerce is utilised that the same brands could make double (we currently have 45% drop off at check out!!!!)

    eCommerce v2 starts now..... get in touch if your v v v good!
  • Is it just me or does Magento's API seem to be missing some critical methods? For example, I see no sales_order.create method that I would utilize to pull eBay checkouts into Magento. Also, I see no methods that would allow me to create attributes and attribute sets. That would save me a crap-load of time configuring Magento for similar clients. Are these methods really not included or am I just missing them in the documentation somehow?
  • penny
    Hi bros,

    I have a project that I need to export a set of data(in XML or via SOAP) from a java based system to magento. Could you please tell me what is the basic idea behind that?

    thanks
    penny
  • BC
    Thanks! Worked great!
  • Saggy
    Great work Branko. Keep blogging!!!
  • Nehal
    I am looking for one method which is not in magento order API that is to create. Rest all methods are available.

    Does anyone implemented it?
  • Yes, could you possibly detail where the code should go, while I'm surfing through other articles on your site.

    Thanks in advance.

    P.S. Branko Ajzele, you did good work, opening this article...
    Also found other interesting topics..
  • vince nguyen
    Hi, I am very new to magento and interested in integrating Magento with a Rest web service that a vendor put out. I saw this example and was able to follow through with all the screenshots. I was reading through this tutorial and did not know where this code suppose to go. Can you point me to to put it?

    $client = new SoapClient(’http://somesite.domain/shop/index.php/api/?wsdl’);

    $session = $client->login(’sampleuser’, ‘123456′);
    $result= $client->call($session, ‘customer.list‘, array());

    echo ‘’;
    print_r($result);
    echo ‘’;
  • Jon
    for anyone else pulling their hair out trying to walk through the core magento files or guess and checking... the only example I could find for detailed arguments as an array passed to an api call was 'like' => '%search%term%'

    I was particularly looking to grab orders by date... after trying many, what I believed to be logical approaches considering mysql queries, eventually what worked was the following:

    $args = array(
    'fieldname' => array(
    'from' => date('Y-m-d h:m:s', strtotime('yesterday')),
    'to' => date('Y-m-d h:m:s', time())
    )
    );

    or something to that effect... so $client->call($session, 'sales_order.list', $args) would give you orders between yesterday and today (technically arguable) but you you get the point. hope this helps someone
  • Jon
    Trying to get fancy with this and getting really frustrated for the lack of documentation on the available/valid arguments to pass to individual methods... any suggestions Branko?
  • Yes, you are right, there is no API folder. I'm not sure I even mentioned one in this article? API is a collection of functions. Not sure what you are trying to do here?

    Basically you need to call one of those functions (methods)... In my article you have a link to Magento Core API documentation. Check it out.
  • Ahsan Shahzad
    if i am not wrong, Konstantin [Moderator] referred "api" in this line:

    $client = new SoapClient(‘http://somesite.domain/shop/index.php/api/?wsdl’);

    I think intention was to know the directory location in which Magento Api related files are being stored.
  • hi guys, i installed magento, but there is no "api" folder in the root, where i can get this folder? or i missed something?

    thanks in advance!
  • Hi, your post helped me a great deal, and I've referenced it in my post about using Web Service to export Magento Orders: http://www.westwideweb.com/wp/2008/12/10/magent...

    Thanks!

    MXWest
  • This is one of those really, really easy examples...

    To be honest if you do not where to write the code I supplied here, then I'm a fried you're not Magento ready. I hope you won't get this to personal, It's just I have been getting a lot of newbie questions about Magento which are not Magento related at all.

    Question "where to write this code" is not Magento related since i see no point in consuming a web service in the system that provides it.

    You use the code provided from within some of your application. Or you can simply create a single PHP file with nothing but the code provided and call that file from your browser.

    Better yet my answer to your question would be: Where have you tried to call it from?

    As I said earlier, please do not get this somewhat harsh answer to personal. It's just I'd rather see some more serious questions and requests under the comment section.

    Anyhow... I appreciate your input. Hope you find ActiveCodeline to be useful. All the best.
  • Shashank
    Hi,
    I am trying to implement this example. I have created User and Role successfully. But I am not getting that where to write the sample code which is connecting to the URL and calling the Function.

    Please specify where to write this code and how to run the page to execute the code.
    Thanks!
  • Pointlessjon
    @Frank: I suppose you could in a sense that you could write a class/script to grab the data you want to post out of magento and send it somewhere -- and dump some code utilizing curl or otherwise into a template in magento where you need to call it... ?
  • Frank
    Great write up!

    What about going the other way and constructing a GET or POST request from Magento TO a 3rd party service?
  • Awesome tutorial! Crystal clear my friend! :)
  • Pointlessjon
    excellent. was just reading through the api yesterday and whaddya know -- you got a tutorial up. thanks again branko
blog comments powered by Disqus
Powered by Wordpress | Designed by Elegant Themes