Question

I'm interacting with PHP's SOAP client to send and receive requests to a remote SOAP server and one of the XML documents that I have to send with the request has duplicate sections. By that I mean the XML is like this:

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

As you can see, the line_data section is duplicating for each line on the invoice. This presents a problem when I'm building the array to send in the SOAP request as line_data would be the array key, and PHP arrays have to have a unique array key.

For example, I can't do this:

$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' => []
];

Does anyone know a way around this? If at all possible I'd like to keep constructing my request XML with arrays because it's done elsewhere in the codebase that way.

Was it helpful?

Solution

Fixed the issue, turns out the PHP SOAP client has anticipated this and allowed us to do the following:

'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]
]

So just supply a multi-dimensional array and the SOAP client will automatically format it correctly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top