简体   繁体   中英

Headers for connecting to a SOAP webservice using PHP

I am trying to connect to a SOAP webservice using php. I am very new to using php.

I can connect to the service, the test below returns a list of all the available functions of the webservice.

$url = "http://...client_ip.../dkServiceDefault/dkWSItemsCGI.exe/wsdl/IItemService";
$client = new SoapClient($url);
var_dump($client->__getFunctions());

If I try to access one of these functions(ex. NumberOfModifiedItems) then I get an error stating that I need to supply a SOAP header with a username and password.

According to the documentation of the SOAP service the header needs to look like this:

<soap:Header>
    <q1:BasicSecurity id="h_id1" xmlns:q1="urn:dkWSValueObjects">
        <Username xsi:type="xsd:string">username</Username>
        <Password xsi:type="xsd:string">password</Password>
    </q1:BasicSecurity>
</soap:Header>

How can I make this header in php? How do I attach it to the SoapClient? I have a username and password but I can't figure out how to create the exact header to send to the webservice. I have tried following several tutorials, but I just can't seem to get it to work.

You may pass SOAP headers with SoapHeader class and SoapClient::__setSoapHeaders method:

<?php
$url = "http://...client_ip.../dkServiceDefault/dkWSItemsCGI.exe/wsdl/IItemService";
$client = new SoapClient($url);

$namespace = "urn:dkWSValueObjects";
$authentication = array(
    'Username' => 'yourname',
    'Password' => 'yourpassword'
);
$header = new SoapHeader($namespace, 'BasicSecurity', $authentication, false);
$client->__setSoapHeaders($header);

var_dump($client->__getFunctions());
?>

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