使用 PHP 和 NuSOAP 通过 HTTPS 发出 SOAP 请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5858080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:42:45  来源:igfitidea点击:

Making a SOAP request over HTTPS using PHP and NuSOAP

phpweb-servicessoaphttpsnusoap

提问by Erik Frisk

I've been stuck with this for days now and I can't seem to find the answer anywhere, even though I think it's a fairly common task. Can anyone help?

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

I'm trying to use PHP and NuSOAP to connect to a web service. The web service requires me to both use an ssl certificate, and pass authentication parameters in the soap request. The request is supposed to look like this according to the admins of the web service:

我正在尝试使用 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>

I've been trying to figure out how to do this using NuSOAP. Right now, this is what I've got (I don't know how to pass the authentication parameters, or use the certificate files I have):

我一直试图弄清楚如何使用 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>';

From using the debugging code above, I can see that this is the response I get from the web service:

通过使用上面的调试代码,我可以看到这是我从 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>

This response is of course not surprising, since I'm not passing any authentication parameters in the request.

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

Does anyone know how I can pass the username and password in the Header of the SOAP request, as well as how I use the ssl certificate files I have? As you can probably tell, I'm pretty new to web services and NuSOAP. If something in my question doesn't make sense, please let me know.

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

采纳答案by Mustafa

if you don't need to you nu_soap you can solve the problem via below script

如果你不需要 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();
?>