2011-09-15 75 views
0

我是PHP編程的完整工程師,但他的任務是編寫一個使用我公司的Java服務編寫的Web服務客戶端。我們有兩種類型的服務。那些需要認證和那些沒有。我已經使用wsdl2php庫成功地與我們不需要身份驗證的服務進行通信。我們的安全服務要求我一個SOAP頭添加到我的請求,看起來像這樣:使用PHP和wsdl2php添加SOAP標頭

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sen="https://webservices.averittexpress.com/SendWebImageService"> 
    <soapenv:Header xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
     <ns:authnHeader soapenv:mustUnderstand="0" xmlns:ns="http://webservices.averittexpress.com/authn"> 
      <Username>xxxxxxxx</Username> 
      <Password>xxxxxxxx</Password> 
     </ns:authnHeader> 
    </soapenv:Header> 

我有問題是,我不知道如何與wsdl2php做到這一點,或者如果它甚至有可能。這是我目前用來測試的PHP文件。

<?php 

require_once 'LTLRateQuoteService.php'; 

$rqs = new LTLRateQuoteService(); 
$info = new ShipmentInfo(); 
$HeaderSecurity = array("authnHeader"=>array("UserName"=>"xxxxxx", 
              "Password"=>"xxxxxx",)); 

$header[] = new SoapHeader("http://schemas.xmlsoap.org/soap/encoding", 
          "Header",$HeaderSecurity); 

$rqs->__setSoapHeaders($header); 

$args = new AvtLTLRateQuoteRequest(); 
$args->AccountNumber = '1234578'; 
$args->OriginCity = 'Cookeville'; 
$args->OriginState = 'TN'; 
$args->OriginZip = '38501'; 
$args->DestinationCity = 'Morristown'; 
$args->DestinationState = 'TN'; 
$args->DestinationZip = '37814'; 
$args->ShipDate = '12/12/2011'; 
$args->Customer = 'S'; 
$args->PaymentType = 'P'; 

$info->NumPieces = '1'; 
$info->NumHandling = '1'; 
$info->CubicFeet = '1'; 
$info->Items = '1'; 
$info->TotalItem = '1'; 
$info->TotalWeight = '100'; 
$args->ShipmentInfo = $info; 

$grq = new getLTLRate(); 
$grq->arg0 = $args; 

$response = $rqs->getLTLRate($grq); 
$details = $response->return; 
print 'Total Freight Charge: ' . $details->TotalFreightCharge . ' '; 
?> 

我需要的用戶名和密碼,以某種方式連接到頭部上的這一要求,而且不知道該怎麼做。我看了一下wsdl2php網站,在關於如何使用這個庫的文檔方面幾乎沒有。任何幫助,將不勝感激。

感謝,

安德魯

+0

爲什麼完整的newbs進入web服務?或者,也許你的廚師非常聰明,要求完整的newb寫Web服務? –

+0

我不是在寫Web服務。我們是一個Java家。我們用Java編寫了Web服務。我們有使用我們的服務的客戶使用PHP來連接它。我只是試圖在PHP中編寫一個簡單的客戶端,作爲如何執行此操作的示例。我得到了不安全的客戶端正常工作。我只是需要認證的麻煩。 –

+0

我有這個問題可以任何一個幫助我 –

回答

1

不要聽粗魯評論巨魔沒有答案,只有侮辱。這是我是如何做到的。

function __construct($url='wsdl url') 
{ 
    $auth=array('AccountUsername'=>'test','AccountPW'=>'test'); 
    $authvalues = new \SoapVar($auth, SOAP_ENC_OBJECT); 
    $header = new \SoapHeader('http://www.nsurl.com/', 
           'AuthenticationHeader', // Rename this to the tag you need 
           $authvalues, false); 

    $this->soapClient = new SoapClient($url,array("classmap"=>self::$classmap,"trace" => true,"exceptions" => true)); 
    $this->soapClient->__setSoapHeaders(array($header)); 
    //set the Headers of Soap Client. 
    //$this->soapClient->__setSoapHeaders($header); 
}