4

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.

3

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.

  • I tried the same but it failed with error. Fatal error: Uncaught SoapFault exception: [SOAP-ENV:Server] SoapServer::SoapServer(): Invalid parameters – Paul Jul 30 '16 at 7:32
  • @magento two: Can you please share your code for Soap connection in M2 custom module if possible. – Purushotam Sharma Dec 12 '17 at 5:42
  • @PurushotamSharma You mean code for invoking Soap API in M2? – Bojjaiah Dec 12 '17 at 5:50
  • Yes, I also want to establish soap connection in my custom M2 module – Purushotam Sharma Dec 12 '17 at 6:01
  • @PurushotamSharma Create a thread and tag me. – Bojjaiah Dec 12 '17 at 7:00
1
        $token = 'your-access-token';

        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');

        $opts = ['http' => ['header' => "Authorization: Bearer " . $token]];

        $context = stream_context_create($opts);


        $wsdlUrl = YOUR_BASEURL."soap?wsdl&services=learningCalculatorWebServiceCalculatorV1";
        $addArgs = array('num1'=>2, 'num2'=>1);
        $sumArgs = array('nums'=>array(2,1000));

        try{
            $soapClient = new \Zend\Soap\Client($wsdlUrl);
            $soapClient->setSoapVersion(SOAP_1_2);
            $soapClient->setStreamContext($context);

            $soapResponse = $soapClient->learningCalculatorWebServiceCalculatorV1Add($addArgs);
            print_r($soapResponse);//3

            $soapResponse = $soapClient->learningCalculatorWebServiceCalculatorV1Sum($sumArgs);//1002
            print_r($soapResponse);
        }catch(Exception $e){
            echo 'Error : '.$e->getMessage();
        }
  • It does not work and return this error Fatal error: Uncaught SoapFault exception: [SOAP-ENV:Server] SoapServer::SoapServer(): Invalid parameters – Paul Jul 30 '16 at 7:42
0

To create soap api call to get product from sku using magento 2 default API service :

$request = new SoapClient('http://192.168.0.54/magento20sample/index.php/soap/default?wsdl&services=catalogProductRepositoryV1',
            array(
                'soap_version' => SOAP_1_2,
                'stream_context' => stream_context_create(array('http'=> array('header' => 'Authorization: Bearer w9vqx2uyenkbvbim6mwochwapq714yox')
                    )                               
                )
            )
        );
        $response = $request->catalogProductRepositoryV1Get(array('sku'=>'test'));

If you want to make full costume module for API in magento 2 refer this : https://www.safaribooksonline.com/library/view/magento-2-developers/9781785886584/ch09s08.html#ch09lvl3sec14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.