[英]Making a SOAP request over HTTPS using PHP and NuSOAP
我已經堅持了好幾天了,我似乎無法在任何地方找到答案,即使我認為這是一項相當普遍的任務。 任何人都可以幫忙嗎?
我正在嘗試使用 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 請求的 Header 中傳遞用戶名和密碼,以及如何使用我擁有的 ssl 證書文件? 正如您可能知道的那樣,我對 web 服務和 NuSOAP 還很陌生。 如果我的問題中的某些內容沒有意義,請告訴我。
如果您不需要 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();
?>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.