Use SOAP API in Magento 2 – How to Guide

apimagento2soap

In Magento1, we use a username/ password in the SOAP call for authentication, what is the difference in Magento2?

For example, When i want to get an order info, I use
http://example.com/soap/en?services=salesOrderRepositoryV1

I got the WSDL request sample like this:

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:en="http://k11.dev.cleargo.com/soap/en?services=salesOrderRepositoryV1">
   <soap:Header/>
   <soap:Body>
      <en:salesOrderRepositoryV1GetRequest>
         <id>1</id>
      </en:salesOrderRepositoryV1GetRequest>
   </soap:Body>
</soap:Envelope> 

Where should I put the authentication information?

Best Answer

If you want to see how many soap services are enabled in your website, you can run below url,
http://localhost/magentoce27/index.php/soap/default?wsdl_list=1

Change your host entry accordingly. Now to get specific orders details, below is the script which will work for you,
Try to add this script in your projects root folder. Script.php

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

require('vendor/zendframework/zend-server/src/Client.php');
require('vendor/zendframework/zend-soap/src/Client.php');
require('vendor/zendframework/zend-soap/src/Client/Common.php');

$wsdlurl = 'http://10.16.16.190/magentoce27/index.php/soap/default?wsdl&services=salesOrderRepositoryV1';

$token = 'dx2x7pl5mxq9hp8jr8efcl5crv2pk699';


$opts = ['http' => ['header' => "Authorization: Bearer ".$token]];
$context = stream_context_create($opts);
//$arguments =
$serviceArgs = array('searchCriteria'=> 
        array('filterGroups' => 
            array ('filters' =>
                array('field' =>'increment_id',
                      'value' => '000000002' , 
                      'condition_type' => 'eq')
                )
         )
);
$soapClient = new \Zend\Soap\Client($wsdlurl);
$soapClient->setSoapVersion(SOAP_1_2);
$soapClient->setStreamContext($context);
$result = $soapClient->salesOrderRepositoryV1GetList($serviceArgs);//array('searchCriteria' => $serviceArgs));
//$result = $soapClient->customerCustomerRepositoryV1GetById(array('customerId' => 1));
/*'search_criteria'=> 
        array('filter_groups' => 
            array ('filters' =>
                array('field' =>'firstname',
                      'value' => 'Veronica' , 
                      'condition_type' => 'eq')
                )
         )
);*/
echo "<pre>"; print_r($result); 
?>

Here, salesOrderRepositoryV1 will call soap services for sales and it will find increment_id as "000000002" as I have used in search criteria.
I have used, $token = 'dx2x7pl5mxq9hp8jr8efcl5crv2pk699' which you need to create from admin panel using below steps,

System->Integration->Add New Integration.

Once integration is created, activate it and put token in your script file.

Now hit URL, http://10.16.16.190/magentoce27/Script.php and it will give you output as shown in the screen shot.

enter image description here

Related Topic