使用PHP和wsdl2php添加SOAP标头

问题描述:

我是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

我有这个问题可以任何一个帮助我 –

而是然后用wsdl2php,使用PHP的内部SOAP功能更好地工作会?

http://php.net/manual/en/class.soapclient.php

+0

我仍然需要创建标题,这是行不通的。 –

+0

http://www.php.net/manual/en/soapclient.setsoapheaders.php#93460似乎给出指示。 – Drazisil

不要听粗鲁评论巨魔没有答案,只有侮辱。这是我是如何做到的。

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); 
}