2017-06-16 190 views
1

我在PHP中使用SoapClient和我有一個小項目的小問題。肥皂PHP請求與登錄標題

我有以下WSDL提出要求usernae /密碼SOAP頭:

https://www1.gsis.gr/webtax2/wsgsis/RgWsPublic/RgWsPublicPort?WSDL

的說明說我必須對SOAP頭通過用戶名和密碼這樣的:

<?xml version="1.0" encoding="UTF-8"?> 
<env:Envelope 
xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:ns="http://gr/gsis/rgwspublic/RgWsPublic.wsdl" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:ns1="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"> <env:Header> <ns1:Security> <ns1:UsernameToken> <ns1:Username>XXXXXXXXXXXXX</ns1:Username> <ns1:Password>YYYYYYYYYYY</ns1:Password> </ns1:UsernameToken> </ns1:Security> </env:Header> 
<env:Body> 
<ns:rgWsPublicVersionInfo/> 
</env:Body> 
</env:Envelope> 

我使用以下。我嘗試了幾十種不同的方法,但沒有運氣!

$options = array ('trace' => true, 'exceptions' => true, 'connection_timeout' => 5, 'Username' => 'XXXXXXX', 'Password' => 'XXXXXXX'); 
$client = new SoapClient("https://www1.gsis.gr/webtax2/wsgsis/RgWsPublic/RgWsPublicPort?WSDL",$options); 
$params['afmCalledFor'] = 'XXXXXX'; 
$params['afmCalledBy'] = 'XXXXXX'; 
$result = $client->rgWsPublicAfmMethod($params); 
print_r($result); 

...,我總是得到錯誤RG_WS_PUBLIC_TOKEN_USERNAME_NOT_DEFINED

預先感謝您的幫助:)。

亞歷

解歸的幫助後,我的代碼是這樣的(完整代碼):(沒有定義用戶呼叫服務)

$ns = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'; //Namespace of the WS. 

//Body of the Soap Header. 
$headerbody = array(
    'UsernameToken' => array(
    'Username' => '*******', 
    'Password' => '*******' 
) 
); 

//Create Soap Header. 
$header = new SOAPHeader($ns, 'Security', $headerbody); 

$options = array ('trace' => true, 'exceptions' => true, 'connection_timeout' => 5); 
$client = new SoapClient("https://www1.gsis.gr/webtax2/wsgsis/RgWsPublic/RgWsPublicPort?WSDL",$options); 

//set the Headers of Soap Client. 
$client->__setSoapHeaders($header); 

$params['afmCalledFor'] = '**********'; 
$params['afmCalledBy'] = '**********'; 
$result = $client->rgWsPublicAfmMethod($params); 
print_r($result); 

我不斷收到RG_WS_PUBLIC_TOKEN_USERNAME_NOT_DEFINED。

任何幫助表示讚賞。我正處於混沌狀態。

+0

你需要尋找到PHP的SOAPHeaders:http://php.net/manual/en/class.soapheader.php – denormalizer

+0

我看着的SOAPHeaders但不能使它工作:( –

回答

0

http://php.net/manual/en/soapclient.setsoapheaders.php#93460

$ns = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'; //Namespace of the WS. 

//Body of the Soap Header. 
$headerbody = array(
    'UsernameToken' => array(
    'Username' => 'XXXXXXXXXXXXX', 
    'Password' => 'YYYYYYYYYYY' 
) 
); 

//Create Soap Header.  
$header = new SOAPHeader($ns, 'Security', $headerbody);  

//set the Headers of Soap Client. 
$client->__setSoapHeaders($header); 
$result = $client->rgWsPublicAfmMethod($params); 
+0

我守得到相同的錯誤RG_WS_PUBLIC_TOKEN_USERNAME_NOT_DEFINED。 我正在複製代碼從開始到我原來的帖子。如果你能幫助我欠你!!我要瘋了這個東西! –

+0

看起來像缺少'mustUnderstand'屬性 – denormalizer

0

嗯,我發現我自己,我張貼在這裏,萬一別人是不知道如何正確設置身份驗證頭。

$client = new SoapClient("***WSDL_URL***",array('trace' => true,'exceptions' => true, 'connection_timeout' => 1)); 
    $authHeader = new stdClass(); 
    $authHeader->UsernameToken->Username = "**USERNAME**"; 
    $authHeader->UsernameToken->Password = "**PASSWORD**"; 
    $Headers[] = new SoapHeader('http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd', 'Security', $authHeader,TRUE); 
    $client->__setSoapHeaders($Headers); 
$options['**OPTION1**'] = XXXXX 
$options['**OPTION2**'] = XXXXX 
    $result = $client->***METHOD_TO_CALL***($options);