【问题标题】:ws-security and nusoap not workingws-security 和 nusoap 不工作
【发布时间】:2011-03-23 06:54:17
【问题描述】:

对于一个项目,我需要联系另一家公司的网络服务。他们在Soap上做了,而且很详细等等。我发现了变量和类型以及什么方法等。

他们还说我应该在标题中使用带有密码和用户名的 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>

【问题讨论】:

  • 嗨,我有同样的问题,可能是相同的网络服务。您愿意分享您的解决方案吗(假设您找到了解决方案)?
  • 如果我有一个好的 splution 我会的,我现在所做的是建立一个 curl 连接并自己创建整个 xml,然后将其发送到服务器。这行得通,但不是很好。

标签: php soap nusoap


【解决方案1】:

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

$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();

【讨论】:

  • 这不起作用,它一直告诉我:需要对最终收件人采取行动,但邮件中不存在。
  • 轻松获得冠军。他只是想帮忙,仅此而已。
猜你喜欢
  • 1970-01-01
  • 2013-10-26
  • 1970-01-01
  • 2014-11-15
  • 1970-01-01
  • 1970-01-01
  • 2010-10-16
  • 2014-03-02
  • 1970-01-01
相关资源
最近更新 更多