发送重复的XML部分并使用PHP中的数组进行构造

时间:2014-03-04 12:14:27

标签: php xml arrays soap

我正在与PHP的SOAP客户端交互以向远程SOAP服务器发送和接收请求,并且我必须与请求一起发送的XML文档之一具有重复的部分。我的意思是XML就像这样:

<pSalesInvoiceInput>
    <SalesInvoice>
        <invoice_date>[unknown type: string]</invoice_date>
        <due_date>[unknown type: string]</due_date>
        <notes>[unknown type: string?]</notes>
        <line_data>
            <description></description>
            <net_amount>[unknown type: string]</net_amount>
            <vat_amount>[unknown type: string]</vat_amount>
            <nominal_code>[unknown type: string]</nominal_code>
        </line_data>
        <line_data>
            <description></description>
            <net_amount>[unknown type: string]</net_amount>
            <vat_amount>[unknown type: string]</vat_amount>
            <nominal_code>[unknown type: string]</nominal_code>
        </line_data>
    </SalesInvoice>
</pSalesInvoiceInput>

正如您所看到的,line_data部分对于发票上的每一行都是重复的。当我构建要在SOAP请求中发送的数组时,这会产生一个问题,因为line_data将是数组键,并且PHP数组必须具有唯一的数组键。

例如,我不能这样做:

$return = [
    'pSalesInvoiceInput' => [
        'SalesInvoice' => [
            'invoice_date' => $this->invoice_date,
            'due_date' => $this->due_date,
            'notes' => '',
            'line_data => [
                'description' => 'Charge #1',
                'net_amount' => '99.99',
                'vat_amount' => '14.56',
                'nominal_code' => '61'
            ],
            'line_data => [
                'description' => 'Charge #2',
                'net_amount' => '45.99',
                'vat_amount' => '6.56',
                'nominal_code' => '43'
            ],
        ]
    ],
    'pSalesInvoiceOutput' => []
];

有没有人知道解决这个问题的方法?如果可能的话,我想继续使用数组构建我的请求XML,因为它是在代码库的其他地方完成的。

1 个答案:

答案 0 :(得分:0)

修复了这个问题,结果证明PHP SOAP客户端已经预料到这一点,并允许我们执行以下操作:

'line_data' => [
    ['description' => 'Test #1',
    'net_amount' => '50.00',
    'vat_amount' => '10.00',
    'nominal_code' => '6110',
    'glue_house_id' => $this->Houses->id],
    ['description' => 'Test #1',
    'net_amount' => '50.00',
    'vat_amount' => '10.00',
    'nominal_code' => '6110',
    'glue_house_id' => $this->Houses->id]
]

所以只需提供一个多维数组,SOAP客户端就会自动正确格式化。