2017-06-16 158 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);