I have an existing m1 site and a php script which uses the soap API to synchronize product information (read only). It is on a host I do not control and uses the litespeed server.

I have been trying to test the M2 soap API and it is doing my head in. I cannot seem to get the authorization working. Anything needing not authorization works fine, but anything needing authorization does not.

This script is put on the magento2 folder and here's the key piece

    // echo "<p>result? ".$token->result; (that works!)
$opts = array(
            'header' => 'Authorization: Bearer '.$token)
    $context = stream_context_create($opts);
    $services = "catalogProductRepositoryV1"; 


            $client = new SoapClient("http://".$host."/soap/default?wsdl&services=$services",array('soap_version' => SOAP_1_2)); 
            $response = $client->__getFunctions();

It seems that no matter what I try, the part about setstreamcontext fails so I cannot use the authorization. I have tried passing it as a context array element, streamcontext array element, and using the setStreamContext method, and nothing seems to work.

UPDATE: I first tried

   $client = new SoapClient("http://".$host."/soap/default?wsdl&services=$services",array('soap_version' => SOAP_1_2, 'context'=>$context)); 

but that doesn't work either. According to PHP documentation, it should work though, as context for passing http header is a documented option. It should therefore pass the Authorization option through?

I am starting to suspect that the way php is set up on this server does not support this context part.

Which versions of PHP support this? Is there any way to do it with the PHP SOAP rather than Zend?

  • You can first try to test Magento 2 SOAP API with SOAP UI first to see if your Magento instance is correctly set up. Nov 28, 2016 at 12:44
  • my instance is properly set up - everything works until I need to pass the context for authorization.
    – iphigenie
    Nov 28, 2016 at 13:07
  • It does seem like all the examples given only work in the Zend implementation of Soap, rather than the native one.
    – iphigenie
    Nov 28, 2016 at 15:16

1 Answer 1


\SoapClient::setStreamContext is not exists in PHP Soap extension. Looks like you use \Zend\Soap\Client wraper from ZF 2.x. But looks like this class dont have __getFunctions like native \SoapClient

To pass context to native SoapClient use code like

$client = new SoapClient(
    array('soap_version' => SOAP_1_2, 'stream_context'=>$context)
  • Hi KAndy - thanks for this. I also first tried to pass context=> as a parameter to the SOAP client creation, which is supported by native SOAP client, but it does not seem to be working either. I can make it work with the local ZEND version but hopefully there's a way to connect to the Magento API that does not require the entire ZENT stack?
    – iphigenie
    Nov 28, 2016 at 13:16
  • Have you encountered a way to do it with the normal vanilla Soap client? Is it worth asking that question out there or will I just get crickets?
    – iphigenie
    Nov 28, 2016 at 15:20
  • I update answer
    – KAndy
    Nov 28, 2016 at 20:19
  • thanks there's something strange that all the instructions out there tell to do it with "context" when it should be "stream_context"
    – iphigenie
    Nov 29, 2016 at 16:15

Your Answer

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

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