带有SoapClient的PHP Complexe标头()

时间:2017-09-07 15:12:30

标签: php soap

我需要发送Soap请求,但服务器需要复杂的身份验证,我不知道如何创建此标头...

目标肥皂结构:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:int="http://www.gmc.net/Phoenix/Integration">
   <soapenv:Header>
    <wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
      <wsse:UsernameToken wsu:Id="UsernameToken-2" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
        <wsse:Username>user</wsse:Username>
        <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">pass</wsse:Password>
      </wsse:UsernameToken>
    </wsse:Security>
   </soapenv:Header>
   <soapenv:Body>
       <int:ListTicketsRequest>
           <int:Company>test</int:Company>
       </int:ListTicketsRequest>
    </soapenv:Body>
</soapenv:Envelope>

我的Php代码

<?php
try
{
	$wsdl = "https://aaa.wsdl";
	$client = new SoapClient($wsdl, array('trace' => 1));

	$header_part = '
	<wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
		<wsse:UsernameToken wsu:Id="UsernameToken-2" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
			<wsse:Username>user</wsse:Username>
			<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">pass</wsse:Password>
		</wsse:UsernameToken>
	</wsse:Security>
	';
	$soap_var_header = new SoapVar( $header_part, XSD_ANYXML, null, null, null );
	$soap_header = new SoapHeader( 'http://your-target-service.com', 'wsse', $soap_var_header );
	$client->__setSoapHeaders($soap_header);

	$res = $client->ListTickets(array('Company' => 'test'));
}
catch (SoapFault $fault)
{
	echo "SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring}) \n \n";

	echo "====== REQUEST HEADERS =====" . PHP_EOL;
	var_dump($client->__getLastRequestHeaders());
	echo "========= REQUEST ==========" . PHP_EOL;
	var_dump($client->__getLastRequest());

}
?>

我的结果

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://www.gmc.net/Phoenix/Integration" xmlns:ns2="http://your-target-service.com">
	<SOAP-ENV:Header>
		<wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
			<wsse:UsernameToken wsu:Id="UsernameToken-2" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
				<wsse:Username>user</wsse:Username>
				<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">pass</wsse:Password>
			</wsse:UsernameToken>
		</wsse:Security>
	</SOAP-ENV:Header>
	<SOAP-ENV:Body>
		<ns1:ListTicketsRequest>
			<ns1:Company>test</ns1:Company>
		</ns1:ListTicketsRequest>
	</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

问题,功能SoapHeader需要在这里的第一个$ data'http://your-target-service.com'(用于测试)。但我的结构包括原生xmlns:ns1

也许我的所有代码都错了.. 我需要你的帮助!!

谢谢:)

0 个答案:

没有答案