0

我是 PHP 编程的新手,但我的任务是编写一个示例 PHP Web 服务客户端,该客户端使用我公司用 Java 编写的 Web 服务。我们有两种服务。那些需要身份验证的和那些不需要的。我已经使用 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 站点,关于如何使用该库的文档很少。任何帮助,将不胜感激。

谢谢,

安德鲁

4

2 回答 2

2

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

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);
}
于 2016-01-22T18:46:47.220 回答
0

而不是使用 wsdl2php,使用 PHP 的内部 SOAP 函数会更好吗?

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

于 2011-09-15T17:49:17.523 回答