如何在php对象中添加多个数据行

时间:2013-07-06 11:50:55

标签: php xml

付款引擎的SOAP API需要多个RequestParameters,例如customeraccountname和customeraccountnummber:

<soapenv:Body Id="_body">
 <req:TransactionRequest>
  <req:Currency>EUR</req:Currency>
  <req:Services>
   <req:Service Name="directdebit" Action="Pay" Version="1">
    <req:RequestParameter Name="customeraccountname">Joe Doe</req:RequestParameter>
    <req:RequestParameter Name="customeraccountnumber">123456789</req:RequestParameter>
   </req:Service>
  </req:Services>
 </req:TransactionRequest>
</soapenv:Body>

我使用以下PHP代码来尝试实现此目的:

<?php
$TransactionRequest = new Body();
$TransactionRequest->Currency = 'EUR';

$TransactionRequest->Services = new Services();
$TransactionRequest->Services->Service = new Service();
$TransactionRequest->Services->Service->Name= 'directdebit';
$TransactionRequest->Services->Service->Action = 'Pay';
$TransactionRequest->Services->Service->Version = 1;

$TransactionRequest->Services->Service->RequestParameter = new RequestParameter();
$TransactionRequest->Services->Service->RequestParameter->Name = 'customeraccountname';
$TransactionRequest->Services->Service->RequestParameter->_ = 'Joe Doe';

$TransactionRequest->Services->Service->RequestParameter = new RequestParameter();
$TransactionRequest->Services->Service->RequestParameter->Name = 'customeraccountnumber';
$TransactionRequest->Services->Service->RequestParameter->_ = '123456789';


//echo var_dump($TransactionRequest);
print_r($TransactionRequest);

class Body
{
    public $Currency;
    public $Services;
}

class Services
{
    public $Global;
    public $Service;
}

class Service
{
    public $RequestParameter;
    public $Name;
    public $Action;
    public $Version;
}

class RequestParameter
{
    public $_;
    public $Name;
    public $Group;
}
?>

但是,生成的对象不包含第一个RequestParameter:

Body Object
(
    [Currency] => EUR
    [Services] => Services Object
        (
            [Global] => 
            [Service] => Service Object
                (
                    [RequestParameter] => RequestParameter Object
                        (
                            [_] => 123456789
                            [Name] => customeraccountnumber
                            [Group] => 
                        )
                    [Name] => directdebit
                    [Action] => Pay
                    [Version] => 1
                )
        )
)

我怎样才能达到要求的结果?

期待你的帮助,问候罗伯特

1 个答案:

答案 0 :(得分:2)

这是因为你已经覆盖了第一个参数。 您需要使RequestParameter成为一个对象数组。

$TransactionRequest->Services->Service->RequestParameter = new Array();

$TransactionRequest->Services->Service->RequestParameter[0] = new RequestParameter();
$TransactionRequest->Services->Service->RequestParameter[0]->name='customeraccountname';
$TransactionRequest->Services->Service->RequestParameter[0]->_ = 'Jon Doe';

$TransactionRequest->Services->Service->RequestParameter[1] = new RequestParameter();
$TransactionRequest->Services->Service->RequestParameter[1]->name='customeraccountnumber';
$TransactionRequest->Services->Service->RequestParameter[1]->_ = '123456789';

希望这会有所帮助..