2016-06-29 26 views
0

私はのSOAPHeaderクラスの認証資格情報を渡す

次のSOAPヘッダ

<soap:Header> 
    <AuthenticationCredentials> 
    <tusa:Password>XXXXX</tusa:Password> 
    <tusa:Username>XXXXX</tusa:Username> 
    </AuthenticationCredentials> 
</soap:Header> 

がどのように私はのSOAPHeaderクラスにユーザー名とパスワードを渡すことができ考える

<?php 

$xmlstr = <<<XML 
<?xml version="1.0" encoding="UTF-8" ?> 
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:tusa="http://schemas.datacontract.org/2004/07/Tusa.Services.ConsumerConnect" xmlns:tem="http://tempuri.org/"> 
<soap:Header> 
    <AuthenticationCredentials> 
    <tusa:Password>XXXXX</tusa:Password> 
    <tusa:Username>XXXXX</tusa:Username> 
    </AuthenticationCredentials> 
</soap:Header> 
<soap:Body> 
    <tem:AddProduct> 
     <tem:CustomerDetail> 
       <tusa:AddressLine1>52 TEST DRIVE</tusa:AddressLine1> 
       <tusa:City>JOHANNESBURG</tusa:City> 
       <tusa:MaritalStatus>Unknown</tusa:MaritalStatus> 
       //more nodes 
     </tem:CustomerDetail> 
    </tem:AddProduct> 
</soap:Body> 
</soap:Envelope> 
XML; 

以下の形式のXMLファイルを持っています。私はコードの下に使用して試してみましたが、私は

認証資格が必要とされているエラー

try 
{ 
    $auth = array(
     'Username'=>'XXXXX', 
     'Password'=>'XXXXX', 
    ); 

    $client = new \SoapClient('https://test.com//Indirect.svc?wsdl', array('soap_version' => SOAP_1_2, 'trace' => 1)); 

    $actionHeader = new \SoapHeader('http://www.w3.org/2005/08/addressing', Action', http://tempuri.org/IInDirect/AddProduct'); 

    $client->__setSoapHeaders($actionHeader); 

    $response = $client->AddProduct($xmlstr); 
} 

catch (\Exception $e) 
{ 
    echo "Error!"; 
    echo $e -> getMessage(); 
    echo 'Last response: '. $client->__getLastResponse(); 
} 
+0

ような何かを探していると思うが取得しています[SoapHeader :: SoapHeader](http://php.net/manual/en/soapheader.soapheader.php) –

+0

がドキュメントを理解できませんでした。あなたが提供したリンクを使ってこの作品を作るにはどうしたらいいですか? – jaahvicky

答えて

1

私はあなたを持っているあなたは、この

$auth = [ 
    'Username' => 'XXXXX', 
    'Password' => 'XXXXX', 
]; 

$ns = 'http://www.w3.org/2003/05/soap-envelope/'; 

$header = new SOAPHeader($ns, 'AuthenticationCredentials', $auth);   

$client->__setSoapHeaders($header); 
+0

あなたのコードを使用するとまだ動作しません 'エラー!メッセージに指定されたSOAPアクション 'HTTP SOAPアクションと一致しません' http://tempuri.org/IInDirect/AddProduct 'をクリックします。最後の応答:http://www.w3.org/2005/08/addressing/faults:Sendera:ActionMismatchメッセージに指定されたSOAPアクション ''がHTTP SOAPアクションと一致しません。 'http://tempuri.org/IInDirect/AddProduct 'をクリックします。 a:Action' – jaahvicky

+0

私の答えで$ nsを置き換えてみてください。http://tempuri.org/IInDirect/AddProduct –

+0

は、$ nsを置き換えた後も同じエラーになります。 – jaahvicky

関連する問題