Search code examples
phpxmlsoapebay-api

SOAP to XML conversion in PHP


I need to generate the following XML with SOAP:

                    ...
                <InternationalShippingServiceOption>
                        <ShippingService>StandardInternational</ShippingService>
                        <ShippingServiceCost currencyID="USD">39.99</ShippingServiceCost>
                        <ShippingServicePriority>1</ShippingServicePriority>
                        <ShipToLocation>CA</ShipToLocation>
                    </InternationalShippingServiceOption>
                    ...

So I have the following SOAP array in PHP to do this:

$params =   array(
         'InternationalShippingServiceOption' => array(
            'ShippingService'=>'StandardInternational',
            'ShippingServiceCost'=>39.99,
            'ShippingServicePriority'=>1,
            'ShipToLocation'=>'CA',
        )
    )
$client = new eBaySOAP($session); //eBaySOAP extends SoapClient
$results = $client->AddItem($params);

Everything works great, except I am not generating the currencyID="USD" attribute in the ShippingServiceCost tag in the XML. How do I do this?


Solution

  • You don't need to use SoapVar. This works (for me at least):

    $params =   array(
             'InternationalShippingServiceOption' => array(
                'ShippingService'=>'StandardInternational',
                'ShippingServiceCost' => array('_' => 39.99, 'currencyID' => 'USD')
                'ShippingServicePriority'=>1,
                'ShipToLocation'=>'CA',
            )
        )
    

    I'm using this technique with the PayPal SOAP API.