Magento 2 – How to Create SOAP API

magento2-dev-betaphp-5.4restsoap

Today i have created Magento2 REST API, it's working. similarly how to create soap api in magento2?.

app/etc/config.php

'modules' => 
  array (  
    'Learning_CalculatorWebService' => 1,
  ),

app/code/Learning/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Learning_CalculatorWebService" schema_version="2.0.0"/>
</config>

app/code/Learning\CalculatorWebService\Api

<?php

namespace Learning\CalculatorWebService\Api;

interface CalculatorInterface
{
    public function add($num1, $num2);
}

app/code/Learning/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
    <preference for="Learning\CalculatorWebService\Api\CalculatorInterface" type="Learning\CalculatorWebService\Model\Calculator" />
</config>

app/code/Learning/etc/webapi.xml

<?xml version="1.0"?>

<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../app/code/Magento/Webapi/etc/webapi.xsd">


    <route url="/V1/calculator/add/:num1/:num2" method="GET">
        <service class="Learning\CalculatorWebService\Api\CalculatorInterface" method="add"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
    </route>

</routes>

app/code/Learning\CalculatorWebService\Model

<?php
namespace Learning\CalculatorWebService\Model;
use Learning\CalculatorWebService\Api\CalculatorInterface;

class Calculator implements CalculatorInterface
{
    public function add($num1, $num2) {
        return $num1 + $num2;
    }

}

And i have entered url http://192.168.0.52/magento-2/index.php/rest/V1/calculator/add/1/2

it's returning 3 working fine.

Similarly how to create Magento2 SOAP API?

Thanks.

Best Answer

Finally I have acieved to debug getServiceMetadata() function from root/app/code/Magento/Webapi/Model/Soap/Config.php.

/**
     * Retrieve specific service interface data.
     *
     * @param string $serviceName
     * @return array
     * @throws \RuntimeException
     */
    public function getServiceMetadata($serviceName)
    {
        $soapServicesConfig = $this->getSoapServicesConfig();
        \Zend_Debug::dump($soapServicesConfig); exit;
        if (!isset($soapServicesConfig[$serviceName]) || !is_array($soapServicesConfig[$serviceName])) {
            throw new \RuntimeException(__('Requested service is not available: "%1"', $serviceName));
        }
        return $soapServicesConfig[$serviceName];
    }

After writing debug code from above method enter your webservice url from your browser like say http://192.168.0.183/NewMagento2/soap?wsdl&services=customerCustomerRepositoryV1 it will form a wsdl webservices. From this we can find core & custom webservices names.

more information look on Branko Article.