【问题标题】:Making a SOAP request over HTTPS using PHP and NuSOAP使用 PHP 和 NuSOAP 通过 HTTPS 发出 SOAP 请求
【发布时间】:2011-08-17 00:23:36
【问题描述】:

我已经被这个问题困扰了好几天了,我似乎无法在任何地方找到答案,尽管我认为这是一项相当普遍的任务。有人可以帮忙吗?

我正在尝试使用 PHP 和 NuSOAP 连接到 Web 服务。 Web 服务要求我同时使用 ssl 证书,并在soap 请求中传递身份验证参数。根据 Web 服务的管理员,请求应该如下所示:

<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Header>
        <o:Security xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
            <o:UsernameToken>
                <o:Username>username</o:Username>
                <o:Password>password</o:Password>
            </o:UsernameToken>
        </o:Security>
    </s:Header>
    <s:Body>
        <PriceQuote xmlns="a_url">
            <PartnerProfileId>a_unique_partner_id_we_have</PartnerProfileId>
            <Reservation xmlns:d4p1="another_url" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">

                * a bunch of stuff

            </Reservation>
        </PriceQuote>
    </s:Body>
</s:Envelope>

我一直试图弄清楚如何使用 NuSOAP 来做到这一点。现在,这就是我所拥有的(我不知道如何传递身份验证参数,或者使用我拥有的证书文件):

$client = new nusoap_client("https://endpoint_url");
$client->soap_defencoding = 'UTF-8'; 

$result = $client->call("PriceQuote", "");

// For debugging
echo '<h2>Request</h2>';
echo '<pre>' . htmlspecialchars($client->request, ENT_QUOTES) . '</pre>';
echo '<h2>Response</h2>';
echo '<pre>' . htmlspecialchars($client->response, ENT_QUOTES) . '</pre>';
echo '<h2>Debug</h2>';
echo '<pre>' . htmlspecialchars($client->debug_str, ENT_QUOTES) . '</pre>';

通过使用上面的调试代码,我可以看到这是我从 Web 服务得到的响应:

HTTP/1.1 500 Internal Server Error
Date: Mon, 02 May 2011 13:42:14 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: 347

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body>
        <s:Fault>
            <faultcode xmlns:a="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
                a:InvalidSecurity
            </faultcode>
            <faultstring xml:lang="sv-SE">
                An error occurred when verifying security for the message.
            </faultstring>
        </s:Fault>
    </s:Body>
</s:Envelope>

这个响应当然不足为奇,因为我没有在请求中传递任何身份验证参数。

有谁知道我如何在 SOAP 请求的标头中传递用户名和密码,以及如何使用我拥有的 ssl 证书文件?您可能会说,我对 Web 服务和 NuSOAP 还很陌生。如果我的问题中的某些内容没有意义,请告诉我。

【问题讨论】:

    标签: php web-services soap https nusoap


    【解决方案1】:

    如果您不需要 nu_soap,您可以通过以下脚本解决问题

    <?PHP
    $client = new SoapClient("http://Your Wsdl Location", array("trace" => 0));
    $HeaderSecurity = 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",$HeaderSecurity);
    
    $client->__setSoapHeaders($header);
    //$client->__setLocation("https://YourTargetLocation/"); if you have wsdl file you don't need to use this line 
    $REsponse = $client->YourRequest();
    ?>
    

    【讨论】:

    • 感谢穆斯塔法的快速反应。使用这个(由于我在没有 wsdl 文件的情况下工作而稍作修改)我仍然收到错误:致命错误:未捕获的 SoapFault 异常:[a:InvalidSecurity] 验证消息的安全性时发生错误。这可能是因为我仍然没有使用 ssl 证书文件吗?如果是这样,你知道我怎样才能包含它们吗?
    • 也许您可以尝试直接传递 xml 行(这是用于 nu_soap):$client-&gt;call('MethodName',$RequestXml,NULL,"MethodName",NULL,NULL,"document");$RequestXml 中传递 xml 并在示例中输入您的 MethodNames。
    • 谢谢!我修改了您的建议以包括标题 xml:$result = $client-&gt;call("PriceQuote",$RequestBody,NULL,"PriceQuote",$RequestHeader,NULL,"document");。这给了我一个新错误:...EndpointDispatcher 的ContractFilter 不匹配。这可能是因为合约不匹配(发送方和接收方之间的操作不匹配)或发送方和接收方之间的绑定/安全不匹配。检查发送方和接收方是否具有相同的合同和相同的绑定(包括安全要求,例如消息、传输、无)。 可能是缺少 SSL 证书吗?
    • 不幸的是不是:(但这不是已知的东西,也不是你在网上找到文档的东西。
    • 事实证明,最终的错误是由于soap请求的格式(Web服务不喜欢这种格式并以内部服务器错误响应)。不过,Mustafa 在 cmets 中的建议确实很有帮助!我最后还是用了他的方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-01
    • 2013-02-01
    • 2011-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多