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.

Leave a Reply

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