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.

Using Magento SOAP API With A Custom Code

June 11, 2021

4 Min Read

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

Scalable Hosting to Launch & Manage Magento Store

Set up your Magento store on the cloud solution of your choice. Get one migration completely FREE.

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.

<?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.

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

Share This Article

Customer Review at

“Great speed, features, knowledgebase, dashboard, UX and fast, expert support. Very happy!”

Stefan [Management Consultant]

Fayyaz Khattak

Fayyaz, a passionate Motorbike tourist, works as a Team Lead — Magento Community at Cloudways - A Managed Magento Hosting Platform. His objective is to learn & share about PHP & Magento Development in Community. You can contact him at [email protected]

×

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

Thankyou for Subscribing Us!

×

Webinar: How to Get 100% Scores on Core Web Vitals

Join Joe Williams & Aleksandar Savkovic on 29th of March, 2021.

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!

Do you like what you read?

Get the Latest Updates

Share Your Feedback

Please insert Content

Thank you for your feedback!