Magento 2.1 – How to Generate Token for SOAP API

magento-2.1soaptoken

I tried to generate token for magento2 SOAP API but i am getting below error

Error:

Fatal error: Uncaught SoapFault exception: [SOAP-ENV:Server] SoapServer::SoapServer(): Invalid parameters in /var/www/html/magento2011/test.php:36 Stack trace: #0 /var/www/html/magento2011/test.php(36): SoapClient->__call('integrationAdmi...', Array) #1 {main} thrown in /var/www/html/magento2011/test.php on line 36

Script:

<?php 
   ini_set('display_errors', 1);
   $params  = array('username' => 'soapadmin','password' => 'test@1234');
   $request = new SoapClient('http://127.0.0.1/magento2011/soap/default?wsdl&services=integrationAdminTokenServiceV1',array('soap_version' => SOAP_1_2));
   $token = $request->integrationAdminTokenServiceV1CreateAdminAccessToken($params); 
 ?>

Best Answer

Try below code, this works for me:-

$request = new SoapClient("http://localhost/magento2sample/soap/?wsdl&services=integrationAdminTokenServiceV1");
 $token = $request->integrationAdminTokenServiceV1CreateAdminAccessToken(array("username"=>"XXX", "password"=>"XXX"));

    $opts = array(
                'http'=>array(
                    'header' => 'Authorization: Bearer '.json_decode($token->result)
                )
            );

    $wsdlUrl = 'http://localhost/magento2sample/soap/default?wsdl&services=serviceName';

    $context = stream_context_create($opts);
    $soapClient = new SoapClient($wsdlUrl, ['version' => SOAP_1_2, 'context' => $context]);

    $soapResponse = $soapClient->__getFunctions();
Related Topic