2

I tried to call SOAP API of Magento 2 but I could not call successfully.

Here is my source code:

require('vendor/zendframework/zend-server/Zend/Server/Client.php');
require('vendor/zendframework/zend-soap/Zend/Soap/Client.php');
require('vendor/zendframework/zend-soap/Zend/Soap/Client/Common.php');

$token = "olourdje47c5c5g66md4gfceyslo9y1p";
$opts = ['http' => ['header' => "Authorization: Bearer " . $token]];
$context = stream_context_create($opts);

$wsdlUrl = "http://mage2.dev/soap?wsdl&services=customerCustomerRepositoryV1";
$serviceArgs = array("customerId" => 1);

$soapClient = new \Zend\Soap\Client($wsdlUrl);
$soapClient->setSoapVersion(SOAP_1_2);
$soapClient->setStreamContext($context);
$soapResponse = $soapClient->customerCustomerRepositoryV1GetById($serviceArgs);
var_dump($soapResponse);

The error message shown on browser is:

Fatal error: Uncaught SoapFault exception: [env:Sender] Consumer is not authorized to access %resources in /vagrant/data/magento2/vendor/zendframework/zend-soap/Zend/Soap/Client.php:1118 Stack trace: #0 /vagrant/data/magento2/vendor/zendframework/zend-soap/Zend/Soap/Client.php(1118): SoapClient->__soapCall('customerCustome...', Array, NULL, NULL, Array)

1 /vagrant/data/magento2/test_api.php(28): Zend\Soap\Client->__call('customerCustome...', Array) #2

/vagrant/data/magento2/test_api.php(28): Zend\Soap\Client->customerCustomerRepositoryV1GetById(Array) #3 {main} thrown in /vagrant/data/magento2/vendor/zendframework/zend-soap/Zend/Soap/Client.php on line 1118


The token in the source code was the access token of the integration with all resource I created on Magento back-end. The integration was active.

I could call the SOAP API without resource need successfully. For example, the Get function in the catalogProductRepositoryV1 service.

I wonder if there are any missing procedures I have to complete first for using SOAP API with resource need.

I will thank you so much for any helps.

2
  • have you found magento1 wsdl.xml file similarly in magento2? Oct 1, 2015 at 11:13
  • @amitbera, have you found magento1 wsdl.xml file similarly in magento2? Oct 1, 2015 at 11:14

3 Answers 3

4

I found the root cause. I did not miss any procedures for the API configuration.

The problem is I am using the VM provided by Magento for running the Magento2. The function apache_request_headers does not work in the VM environment so that the php cannot get "Authorization" in the header of the HTTP request.

I spent a whole afternoon for debugging.

2
  • wow... I ran into the same issue a while back and gave up on it. Thanks for that info. I noticed they've since pulled that VM entirely.
    – Ryan Hoerr
    Oct 1, 2015 at 23:48
  • I'm glad that this helped you.
    – Jason Yip
    Oct 4, 2015 at 13:56
2

click on System -> Integration Menu it will show you list api users. From that edit anyone user it will show you as below screen shot from that copy Access Token and assign to the $token variable.

And run your code it should work.

enter image description here

1

In your case file path are wrong. Try the below code

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 = "olourdje47c5c5g66md4gfceyslo9y1p";
$opts = ['http' => ['header' => "Authorization: Bearer " . $token]];
$context = stream_context_create($opts);

$wsdlUrl = "http://mage2.dev/soap?wsdl&services=customerCustomerRepositoryV1";
$serviceArgs = array("customerId" => 1);
try{  
$soapClient = new \Zend\Soap\Client($wsdlUrl);
$soapClient->setSoapVersion(SOAP_1_2);
$soapClient->setStreamContext($context);
$soapResponse = $soapClient->customerCustomerRepositoryV1GetById($serviceArgs);
var_dump($soapResponse);
}catch(Exception $e){echo 'Un-authorized Req:'.$e->getMessage();}

Note: $token must be the 'Access Token', shown by @magento two in the above post.

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.