How to Get Order & Customer Information By Magento 2 SOAP API
Vinh Jacker | 06-21-2024
SOAP API in Magento 2 is an application programming interface based on the SOAP (Simple Object Access Protocol) protocol, making it easy for developers to access and manage data on the system. Using the SOAP API not only helps automate processes but also enhances the efficiency of managing order and customer information.
In this article, we will guide you through the simple steps to get customer information and order information using the SOAP API in Magento 2.
Make sure the SOAP APIs are enabled and properly configured in your Magento 2 instance.
Steps to Get Order Information Using SOAP API in Magento 2
When you need to create a mobile application for your Magento website, you will need order information from outside the store, and the SOAP API will help you with this.
To get order information using SOAP API in Magento 2, create a file called call_soap.php and add the following code:
<?php
$request = new SoapClient("http://<magento_host>/soap/?wsdl&services=integrationAdminTokenServiceV1", array("soap_version" => SOAP_1_2));
// changes your store url.
$token = $request->integrationAdminTokenServiceV1CreateAdminAccessToken(array("username"=>"admin2", "password"=>"admin123"));
$request = new SoapClient(
'http://<magento_host>/soap/default?wsdl&services=salesOrderRepositoryV1',
array(
'soap_version' => SOAP_1_2,
'stream_context' => stream_context_create(array(
'http'=> array('header' => 'Authorization: Bearer '.$token->result)
))
)
);
// changes your store url.
$response = $request->SalesOrderRepositoryV1Get(array('id' => 1));
var_dump($response);
?>
With just one step, you can get order information using the SOAP API in Magento 2. Additionally, the SOAP API can help you maintain products and inventory for your Magento 2 store. Integrate the SOAP API to synchronize inventory for Magento 2 to achieve this.
Steps to Get Customer Information Using SOAP API in Magento 2
Similar to getting order information, you can also get customer information using the SOAP API in Magento 2.
To do this, create a file named call_soap.php and enter the code below:
<?php
$request = new SoapClient("http://<magento_host>/soap/?wsdl&services=integrationAdminTokenServiceV1", array("soap_version" => SOAP_1_2));
// changed your store url.
$token = $request->integrationAdminTokenServiceV1CreateAdminAccessToken(array("username"=>"admin", "password"=>"admin@1234"));
$request = new SoapClient(
'http://<magento_host>/soap/default?wsdl&services=customerCustomerRepositoryV1',
array(
'soap_version' => SOAP_1_2,
'stream_context' => stream_context_create(array(
'http'=> array('header' => 'Authorization: Bearer '.$token->result)
))
)
);
// you need pass customer id in array
$response = $request->customerCustomerRepositoryV1GetById(array("customerId" => 3));
echo "<pre>";
print_r($response);
echo "</pre>";
?>
In this way, you can easily get detailed customer information using the SOAP API in Magento 2.
Conclusion
Retrieving customer and order information using the SOAP API in Magento 2 not only helps automate the management process but also enhances efficiency and accuracy in data management.
We hope that through this article, you have grasped the basic steps to perform API requests and integrate them into your system effectively.
If you encounter any difficulties during the implementation process, do not hesitate to contact us for support.