ws-security和nusoap无法正常工作

问题描述 投票:0回答:1

对于一个项目,我需要联系其他公司的webservice。他们已经在Soap上制作了它,并且它都是详细的等等。我发现了变量和类型以及什么方法等。

他们还说我应该在头文件中使用带有pwd和username的WS-security。

所以我用nusoap做了这个:

require_once('../lib/nusoap.php');
$client = new nusoap_client("http://webservice.client.com/cir.asmx?WSDL", 'wsdl');
$err = $client->getError();
if ($err) {
    echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
}
$client->soap_defencoding = 'UTF-8';

$result = $client->call('GetLastUpdate');
// Check for a fault
if ($client->fault) {
    echo '<h2>Fault</h2><pre>';
    print_r($result);
    echo '</pre>';
} else {
    // Check for errors
    $err = $client->getError();
    if ($err) {
        // Display the error
        echo '<h2>Error</h2><pre>' . $err . '</pre>';
    } else {
        // Display the result
        echo '<h2>Result</h2><pre>';
        print_r($result);
        echo '</pre>';
    }
}
echo '<h2>Request</h2><pre>' . htmlspecialchars($client->request, ENT_QUOTES) . '</pre>';
echo '<h2>Response</h2><pre>' . htmlspecialchars($client->response, ENT_QUOTES) . '</pre>';
echo '<h2>Debug</h2><pre>' . htmlspecialchars($client->debug_str, ENT_QUOTES) . '</pre>';

然后我补充说:

$client->setCredentials('******','*******','basic');

这不起作用,所以我查找了ws-security样式并添加了以下内容:

$auth='<wsse:Security SOAP-ENV:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:UsernameToken>
    <wsse:Username>********</wsse:Username>
    <wsse:Password Type="wsse:PasswordText">*******</wsse:Password>
    <wsse:Nonce>'.base64_encode(pack('H*',$nonce)).'</wsse:Nonce>
    <wsu:Created xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">'.time().'</wsu:Created>
   </wsse:UsernameToken>
</wsse:Security>';

$client->setHeaders($auth);

哪个也不起作用......他们给出了同样的错误....这就是它返回的内容:

Fault

Array
(
    [faultcode] => q0:Security
    [faultstring] => Header http://schemas.xmlsoap.org/ws/2004/08/addressing:Action for ultimate recipient is required but not present in the message.
    [faultactor] => http://webservice.client.com/cir.asmx
)
Request

POST /cir.asmx HTTP/1.0
Host: webservice.client.com
User-Agent: NuSOAP/0.9.5 (1.123)
Content-Type: text/xml; charset=UTF-8
SOAPAction: "http://www.client.com/namespaces/cir01/GetLastUpdate"
Content-Length: 967

<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns2278="http://tempuri.org"><SOAP-ENV:Header><wsse:Security SOAP-ENV:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:UsernameToken>
    <wsse:Username>*******</wsse:Username>
    <wsse:Password Type="wsse:PasswordText">********</wsse:Password>
    <wsse:Nonce></wsse:Nonce>
    <wsu:Created xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">1300862463</wsu:Created>
   </wsse:UsernameToken>
</wsse:Security></SOAP-ENV:Header><SOAP-ENV:Body><GetLastUpdate xmlns="http://www.client.com/namespaces/cir01"></GetLastUpdate></SOAP-ENV:Body></SOAP-ENV:Envelope>
Response

HTTP/1.1 500 Internal Server Error
Connection: close
Date: Wed, 23 Mar 2011 06:39:57 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Cache-Control: private
Content-Type: text/xml; charset=utf-8
Content-Length: 1421

<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"><soap:Header><wsa:Action>http://schemas.xmlsoap.org/ws/2004/08/addressing/fault</wsa:Action><wsa:MessageID>urn:uuid:b8c93511-b6e6-4247-9ab5-65bd4a6aa286</wsa:MessageID><wsa:RelatesTo>urn:uuid:6dfa917b-3163-4ae9-bd84-0855b7a1329e</wsa:RelatesTo><wsa:To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:To><wsse:Security><wsu:Timestamp wsu:Id="Timestamp-4d37a6a5-9445-40d6-8660-a724999cc3bc"><wsu:Created>2011-03-23T06:39:57Z</wsu:Created><wsu:Expires>2011-03-23T06:44:57Z</wsu:Expires></wsu:Timestamp></wsse:Security></soap:Header><soap:Body><soap:Fault><faultcode xmlns:q0="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">q0:Security</faultcode><faultstring>Header http://schemas.xmlsoap.org/ws/2004/08/addressing:Action for ultimate recipient is required but not present in the message.</faultstring><faultactor>http://webservice.client.com/cir.asmx</faultactor></soap:Fault></soap:Body></soap:Envelope>
php soap nusoap
1个回答
0
投票

请尝试使用以下方法设置标头:

$client = new SoapClient("Wsdl_URL", array("trace" => 0)); 

$WSHeader = array(
    "UsernameToken"=>array(
        "Username"=>"YourUserName",
        "Password"=>"YourPassword",
    )
);

$header[] = new
SoapHeader("http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd","Security",$WSHeader);

$client->__setSoapHeaders($header);

$REsponse = $client->YourRequest();
© www.soinside.com 2019 - 2024. All rights reserved.