問題描述
我需要連接到需要純文本用戶名和密碼形式的身份驗證憑據的 Web 服務.
I need to connect to a web service that requires authentication credentials in the form of a plain text user name and password.
我對 SOAP 有基本的了解,并且已經設法使用 NuSOAP 連接到其他不需要用戶名或密碼的開放網絡服務.
I have a basic understanding of SOAP and have managed to connect to other open web services that do not require a username or password using NuSOAP.
以下內容已發送給我:
<?php
// Set up security options
$security_options = array("useUsernameToken" => TRUE);
$policy = new WSPolicy(array("security" => $security_options));
$security_token = new WSSecurityToken(array(
"user" => "xxx",
"password" => "xxx",
"passwordType" => "basic"));
// Create client with options
$client = new WSClient(array("wsdl" => "https://xxx.asmx?wsdl",
"action" => "http://xxx",
"to" => "https://xxx",
"useWSA" => 'submission',
"CACert" => "cert.pem",
"useSOAP" => 1.1,
"policy" => $policy,
"securityToken" => $security_token));
// Send request and capture response
$proxy = $client->getProxy();
$input_array = array("From" => "2010-01-01 00:00:00",
"To" => "2010-01-31 00:00:00");
$resMessage = $proxy->xxx($input_array);
?>
經過一些研究,我了解到上述實現使用 wso2.我需要能夠在不使用 wso2 的情況下做到這一點.
After some research I understand that the above implementation uses wso2. I need to be able to do this without using wso2.
我已盡最大努力尋找有關上述內容的資源(Google、論壇等),但一無所獲.我已經閱讀了一些關于 SOAP 的教程,并且已經能夠使用 PHP 設置一個 SOAP 客戶端,但無法理解所有的身份驗證和策略".
I have tried my best to look for resources (Google, forums, etc) about the above but haven't been able to find anything. I have read some tutorials on SOAP and have been able to set up a SOAP client using PHP but cannot get my head around all the authentication and "policies".
如果我正在扯頭發,我將非常感謝有關如何實現這一點的解釋以及一些進一步閱讀有關此內容的鏈接!理想情況下,我想要一些資源鏈接,供 SOAP 身份驗證的絕對初學者使用.
An explanation of how to achieve this and maybe some links to further reading about this would be very much appreciated as I am tearing my hair out! Ideally I would like some links to resources for an absolute beginner to the SOAP authentication.
謝謝.P.S 上面的一些鏈接/憑據可能已被 xxx'd 用于隱私.
Thanks. P.S some of the links/credentials in the above could have been xxx'd for privacy.
推薦答案
如果你在 php (php version >= 5.0.1) 中啟用了 SOAP 擴展,你可以使用 SoapClient 類來處理您的請求.要進行身份驗證,您可以將用戶名和密碼傳遞給具有目標 URL 的類:
If you have the SOAP extension enabled in php (php version >= 5.0.1), you can use the SoapClient class to process your request. To authenticate, you can pass the username and password to the class with the target URL:
$soapURL = "https://www.example.com/soapapi.asmx?wsdl" ;
$soapParameters = Array('login' => "myusername", 'password' => "mypassword") ;
$soapFunction = "someFunction" ;
$soapFunctionParameters = Array('param1' => 42, 'param2' => "Search") ;
$soapClient = new SoapClient($soapURL, $soapParameters);
$soapResult = $soapClient->__soapCall($soapFunction, $soapFunctionParameters) ;
if(is_array($soapResult) && isset($soapResult['someFunctionResult'])) {
// Process result.
} else {
// Unexpected result
if(function_exists("debug_message")) {
debug_message("Unexpected soapResult for {$soapFunction}: ".print_r($soapResult, TRUE)) ;
}
}
如果您不確定可以調用的函數,您可以在瀏覽器中查看目標 URL(例如以.asmx?wsdl"結尾).您應該得到一個 XML 響應,該響應告訴您可以調用的可用 SOAP 函數以及這些函數的預期參數.
If you're not sure about the functions you can call, you can view the target URL (e.g. ending in ".asmx?wsdl") in your browser. You should get an XML response that tells you the available SOAP functions you can call, and the expected parameters of those functions.
這篇關于使用 PHP 進行 SOAP 身份驗證的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!