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.
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:
- Create SOAP/XML-RPC – Roles
- Create SOAP/XML-RPC – User
- Assign the created role to the user
- Call Magento SOAP API Method
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.
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.
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.
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.
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.
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.
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.
<?php $apisoap_v2_url = "http://subdomain.umrahplan.com/api/v2_soap/?wsdl"; $username = 'administrator'; $password = 'fayyazkhattak'; $client = new SoapClient($apisoap_v2_url); //retreive session id from login $session_id = $client->login($username, $password); //call magentoInfo method $result = $client->magentoInfo($session_id); var_dump($result);
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.
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.
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 firstname.lastname@example.org
Double Your Conversions with Magento Managed Cloud
Free SSL, Free Migration, Optimized Stack, Built-in Varnished Cache and much more.