如何在php中设置SOAP请求以使用SoapClient



我正试图使用php SoapClient来使用此SOAP api,但它似乎不起作用。你们谁能给我指路吗?

这是请求示例:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:par="http://www.example.com/interface/">
<soapenv:Header>
<par:Header>
<par:UserName>USERNAME</par:UserName>
<par:Password>PASSWORD</par:Password>
</par:Header>
</soapenv:Header>
<soapenv:Body>
<par:GetExampResult>
<par:rollNumber>1703011</par:rollNumber>
</par:GetExampResult>
</soapenv:Body>
</soapenv:Envelope>

这就是我试图将其转换为Soap客户端而不起作用的方式。

$username = 'USERNAME';
$password = 'PASS';
$soapURL = "http://www.example.com/interface.asmx?WSDL";
$client = new SoapClient($soapURL);
$auth = [
'SamplingSoapHeader' => [
'UserName' => $username,
'Password' => $password,
],
];
$header = new SoapHeader('http://www.example.com/interface/',
'RequestorCredentials', $auth);
$client->__setSoapHeaders($header);
$response = $client->GetExampResult(["rollNumber" => "1703011"]);
print_r($response);

感谢

您将Soapheader封装到一个额外的XML标记中:

<RequestorCredentials>
<SamplingSoapHeader>
<Username>XYZ</Username>
<Password>123</Password>
</SamplingSoapHeader>
<RequestorCredentials>

在Soapheader ctor中,将Name设置为SamplingSoapHeader,而不是RequestorCredentials,并让$auth是一个仅包含Username和Password的数组,而不是将其包装到SamplingSoapHeader中。

最新更新