简体   繁体   中英

PHP - SOAP API authentication

I encounter some problems when I try to call a method of api with authentication.

 $auth = (object) array(
                'user'=>'username',
                'password'=>'password',
                'company'=> 'companyname'
          );
         
    $url = 'https://webservices.gocadservices.com/NVS?wsdl';
    
    $soap = new SoapClient('https://webservices.gocadservices.com/NVS?wsdl',$auth);
    echo $soap->nop();

I get an error: The user parameter is empty or missing.

My question: How can I send a request xml like this:

<?xml version='1.0' encoding='UTF-8'?> 
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> 
<soapenv:Header> 
<xs:nvheader xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
<xs:nvcommand> 
    <xs:user>
          username
    </xs:user> 
    <xs:password>
         mot de passe
     </xs:password> 
     <xs:company>
          companyname
      </xs:company> 
</xs:nvcommand> 
</xs:nvheader> 
</soapenv:Header> 
</soapenv:Envelope> 

I must have this structure so that the API SOAP will work. Thanks.

You need to set a SOAP Header for this to work:

 $nvcommand = (object)[
'user' => 'username',
'password' => 'password',
'company' => 'company'];

$authHeader = new SoapHeader('namespace','nvcommand', $nvcommand);

$soapClient = new SoapClient('http://localhost:44333/SoapService.com?wsdl', $soapOptions);
$soapClient->__setSoapHeaders($authHeader);

$return = $soapClient->__soapCall('HelloWorld',[]);

This will send a SOAP Header

<env:Header>
    <ns2:nvcommand>
        <user>username</user>
        <password>password</password>
        <company>company</company>
    </ns2:nvcommand>
</env:Header>

along with the Request. The options array in the SoapClient ctor isnt used to pass auth information. Please refer to https://www.php.net/manual/de/soapclient.construct.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM