This website uses cookies

Our website, platform and/or any sub domains use cookies to understand how you use our services, and to improve both your experience and our marketing relevance.

This holiday season, maximize your sales through enhanced website performance and 24/7 caring support. SAVE 30% NOW

How To Use Magento SOAP API With Your Custom Code

Updated on  23rd April

4 Min Read
Reading Time: 4 minutes

Magento is a robust ecommerce platform for scalable businesses. It has made it easier for developers to directly manage resources such as customers, categories, products, and sales orders through APIs. Its APIs also allow the developers to control shopping carts and inventory.

There are two major Web APIs for Magento including REST and SOAP APIs.

Magento SOAP API

In this article, we will focus on SOAP API and make a basic connection using custom code by calling the API method.

SOAP stands for Simple Object Access Protocol. It is based on XML, mostly called HTTP-POST service. The basic structure of SOAP contains an ‘Envelope’ with a ‘Header’ and a ‘Body’ inside.

Magento supports both V1 and V2 versions of SOAP API. To access Magento API via SOAP, you need to follow the following steps:

  1. Create SOAP/XML-RPC – Roles
  2. Create SOAP/XML-RPC – User
  3. Assign the created role to the user
  4. Call Magento SOAP API Method

Let’s start.

Create SOAP/XML-RPC – Roles

First, we will be defining appropriate roles for SOAP web services. Log in to your Magento admin panel, navigate to System > Web Services > SOAP/XML-RPC – Roles and tap Add New Role button.

SOAP Add new Role

Now, enter the Role Name of your choice and also enter the Current Admin Password of your admin panel. Then, click on Role Resources tab.

Role Resource

The Role Resources are defined in each Magento module. Choose the Role Resources you want and click Save Role button. I have selected All from the drop-down Resource Access list.

Resource Access

Create SOAP/XML-RPC – User

After creating a Role, let’s create a User that will have defined permissions to use the Role. Navigate to System > Web Services > SOAP/XML-RPC – Users and tap Add New User button.

SOAP Add New User

Enter the appropriate information required in User Info tab under the Account Information section. Remember that User Name and API Key are the values that we will use on the client side to access the API.

SOAP API Key

Assign Role To The User

Once you have entered all the necessary information, click on User Role tab and assign the role to the user by clicking on the radio button. Once done, tap Save User button.

SOAP Save User

Call Magento SOAP API Method

To connect to Magento SOAP API, load the WSDL into your SOAP client from either of these URLs:

  • URL For Magento SOAP API V1: http://yourdomain.com/api/soap/?wsdl
  • URL For Magento SOAP API V2: http://yourdomain.com/api/v2_soap/?wsdl

Now, create a simple PHP script to login to Magento through the SOAP API and call a method to fetch results.

Note: I have used Magento SOAP API V2 for the test in this blog post.

Create a new PHP file on the root of your Magento installation, or anywhere on the client side and copy paste the following code into it.

Now initialize a new SoapClient object with an argument of the Magento SOAP URL and then log into the API using the credentials that you created earlier.

This will return a session ID that you will reuse for calling the magentoInfo method.

After running the script, you will get the following information on the browser.

Conclusion

Congrats! You have just learned how to connect to Magento SOAP API from your website.

If you are interested in learning more about Magento SOAP API, stay tuned. Or, if you have any questions, feel free to drop me a message or a comment below.

Share your opinion in the comment section. COMMENT NOW

Fayyaz Khattak

Fayyaz is a Magento Community Manager at Cloudways - A Managed Magento Hosting Platform. His objective is to learn & share about PHP & Magento Development in Community. Fayyaz is a food lover and enjoys driving. You can email him at m.fayyaz@cloudways.com

Start Growing with Cloudways Today.

Our Clients Love us because we never compromise on these

Get Our Newsletter
Be the first to get the latest updates and tutorials.

THERE’S MORE TO READ.