1

I get this error when I tried to use soap with the following piece of code.

Error

Uncaught SoapFault exception: [env:Sender] Consumer is not authorized to access %resources in ...

PHP

$wsdlUrl = 'http://local.magento.com/soap/default?wsdl&services=customerAccountManagementV1';
$opts = array('http' => array('header' => 'Authorization: Bearer n95e7at5r0wf6mj1xv6ynni62d4xbkfi'));
$context = stream_context_create($opts);
$soapClient = new SoapClient($wsdlUrl, ['version' => SOAP_1_2, 'context' => $context]);
$serviceArgs = array("customerId" => 1);
$soapResponse = $soapClient->customerAccountManagementV1IsReadonly($serviceArgs);
var_dump($soapResponse);

The token is correct and there is no problem when I test it using soapUI.

Can anyone help?

1 Answer 1

2

You can use below working example and try to follow the provided Magento 2 SOAP service consuming code piece.

In the below example i have taken GetDefaultBillingAddress and passed customerId" => 1 to get the customer default billing address.

Example

Create a file in you Magento root (http://local.magento.com/soap.php), paste below code with your file paths, token and load the file in browser.

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

$token = '5proni6r27e34uywnem5orgu4jstgw3d';

$wsdlUrl = 'http://127.0.0.1/m5agento207/soap/default?wsdl&services=customerAccountManagementV1';
$opts = ['http'=> ['header' => "Authorization: Bearer " .$token]];
$context = stream_context_create($opts);

$soapClient  = new \Zend\Soap\Client($wsdlUrl);

$soapClient->setSoapVersion(SOAP_1_2);
$soapClient->setStreamContext($context);

$slide = array("customerId" => 1);
$result = $soapClient->customerAccountManagementV1GetDefaultBillingAddress(array("customerId" => 1));
var_dump($result);
exit;

Hope this helps.

1
  • It works. But I don't understand. What is the difference between your code and my code? Is it because of the zendframework?
    – TFS
    Jul 31, 2016 at 6:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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