To call SOAP API outside Magento 2:
step1:
app/code/Vendor/Module/Model/Product.php
<?php
namespace Vendor\Module\Model;
use Vendor\Module\Api\ProductInterface;
class Product extends \Magento\Framework\Model\AbstractModel implements ProductInterface{
public function __construct(
.......
){
.......
}
/**
*
* @param int|null $store.
* @return string options in a json format.
*/
public function addTemplateProducts($store){
return 'store:'.$store;//.json_encode($arg_list);
}
}
?>
step:2
app/code/Vendor/Module/Api/ProductInterface.php
<?php
namespace Vendor\Module\Api;
interface ProductInterface
{
/**
*
* @param int|null $store.
* @return string options in a json format.
*/
public function addTemplateProducts($store);
}
?>
step3:
app/code/Vendor/Module/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/product/addTemplateProducts" method="POST">
<service class="Vendor\Module\Api\ProductInterface" method="addTemplateProducts"/>
<resources>
<resource ref="anonymous"/>
</resources>
</route>
</routes>
step4:
your-magento-base-url.com/soapTest.php
<?php
define('BASEPATH','http://localhost/magento21/');
error_reporting(E_ALL & ~E_NOTICE);//ini_set('display_errors',1);//error_reporting(0);
$token = 'tf4ti4s8jjbt7jtpaxh9k2ex8txdf2vs';// Your Integration access token
require_once('vendor/zendframework/zend-server/src/Client.php');
require_once('vendor/zendframework/zend-soap/src/Client.php');
require_once('vendor/zendframework/zend-soap/src/Client/Common.php');
ini_set("soap.wsdl_cache_enabled", 0);
$opts = array('http' => array('header' => "Authorization: Bearer " . $token));
$context = stream_context_create($opts);
$wsdlUrl = BASEPATH."soap?wsdl&services=vendorModuleProductV1";
$param = array('store'=>2);
try{
$soapClient = new \Zend\Soap\Client($wsdlUrl);
$soapClient->setSoapVersion(SOAP_1_2);//SOAP_1_1 or SOAP_1_2
$soapClient->setStreamContext($context);
$soapResponse = $soapClient->vendorModuleProductV1MethodName($param);
print_r($soapResponse);
}catch(Exception $e){
//echo 'Error : '.$e->getMessage();
//echo '<br />'.$e->getTraceAsString();
echo '<br />'.$e->__toString();
//echo '<br />'.$e->getFile();echo '<br />'.$e->getLine();
//print_r($e->getTrace());
//var_dump($e->getCode());
}
Now run your-magento-base-url.com/soapTest.php in the browser and you will see
stdClass Object
(
[result] => store:2
)
I'll update this answer regarding REST call outside Magento 2 soon.
The above worked fine for me. So check and let me know.