在PHP中转换/修改/格式化SOAP调用的XML数据

时间:2011-12-09 21:35:23

标签: php soap-client

我正在使用FoxyCart。我需要通过SOAP将每个订单提供给第三方服务(3PL Central)。我正在使用php SoapClient。

我需要订购此订单:

<foxydata>
<store_version>
    <![CDATA[0.7.2]]>
</store_version>
<transactions>
    <transaction>
        <id>
            <![CDATA[6122236]]>
        </id>
        <store_id>
            <![CDATA[12474]]>
        </store_id>
        <store_version>
            <![CDATA[0.7.2]]>
        </store_version>
        <is_test>
            <![CDATA[1]]>
        </is_test>
        <is_hidden>
            <![CDATA[0]]>
        </is_hidden>
        <data_is_fed>
            <![CDATA[1]]>
        </data_is_fed>
        <transaction_date>
            <![CDATA[2011-11-28 16:31:53]]>
        </transaction_date>
        <payment_type>
            <![CDATA[plastic]]>
        </payment_type>
        <payment_gateway_type>
            <![CDATA[authorize]]>
        </payment_gateway_type>
        <processor_response>
            <![CDATA[Authorize.net Transaction ID:2166733111]]>
        </processor_response>
        <processor_response_details>
        </processor_response_details>
        <purchase_order>
            <![CDATA[]]>
        </purchase_order>
        <cc_number_masked>
            <![CDATA[xxxxxxxxxxxx1111]]>
        </cc_number_masked>
        <cc_type>
            <![CDATA[Visa]]>
        </cc_type>
        <cc_exp_month>
            <![CDATA[09]]>
        </cc_exp_month>
        <cc_exp_year>
            <![CDATA[2013]]>
        </cc_exp_year>
        <cc_start_date_month>
            <![CDATA[]]>
        </cc_start_date_month>
        <cc_start_date_year>
            <![CDATA[]]>
        </cc_start_date_year>
        <cc_issue_number>
            <![CDATA[]]>
        </cc_issue_number>
        <minfraud_score>
            <![CDATA[0]]>
        </minfraud_score>
        <paypal_payer_id>
            <![CDATA[]]>
        </paypal_payer_id>
        <customer_id>
            <![CDATA[2107952]]>
        </customer_id>
        <is_anonymous>
            <![CDATA[0]]>
        </is_anonymous>
        <customer_first_name>
            <![CDATA[Jacob]]>
        </customer_first_name>
        <customer_last_name>
            <![CDATA[Dubail]]>
        </customer_last_name>
        <customer_company>
            <![CDATA[]]>
        </customer_company>
        <customer_address1>
            <![CDATA[2123 4th Ave N]]>
        </customer_address1>
        <customer_address2>
            <![CDATA[]]>
        </customer_address2>
        <customer_city>
            <![CDATA[Seattle]]>
        </customer_city>
        <customer_state>
            <![CDATA[WA]]>
        </customer_state>
        <customer_postal_code>
            <![CDATA[98109]]>
        </customer_postal_code>
        <customer_country>
            <![CDATA[US]]>
        </customer_country>
        <customer_phone>
            <![CDATA[]]>
        </customer_phone>
        <customer_email>
            <![CDATA[jacob@rno1.com]]>
        </customer_email>
        <customer_ip>
            <![CDATA[24.16.147.150]]>
        </customer_ip>
        <shipping_first_name>
            <![CDATA[Jacob]]>
        </shipping_first_name>
        <shipping_last_name>
            <![CDATA[Dubail]]>
        </shipping_last_name>
        <shipping_company>
            <![CDATA[]]>
        </shipping_company>
        <shipping_address1>
            <![CDATA[2123 4th Ave N]]>
        </shipping_address1>
        <shipping_address2>
            <![CDATA[]]>
        </shipping_address2>
        <shipping_city>
            <![CDATA[Seattle]]>
        </shipping_city>
        <shipping_state>
            <![CDATA[WA]]>
        </shipping_state>
        <shipping_postal_code>
            <![CDATA[98109]]>
        </shipping_postal_code>
        <shipping_country>
            <![CDATA[US]]>
        </shipping_country>
        <shipping_phone>
            <![CDATA[]]>
        </shipping_phone>
        <shipto_shipping_service_description>
            <![CDATA[]]>
        </shipto_shipping_service_description>
        <product_total>
            <![CDATA[69.95]]>
        </product_total>
        <tax_total>
            <![CDATA[0]]>
        </tax_total>
        <shipping_total>
            <![CDATA[0]]>
        </shipping_total>
        <order_total>
            <![CDATA[69.95]]>
        </order_total>
        <receipt_url>
            <![CDATA[http://g-form.foxycart.com/receipt?id=a300bc60401cff62b15efe0cb74446d9]]>
        </receipt_url>
        <taxes>
        </taxes>
        <discounts>
        </discounts>
        <customer_password>
            <![CDATA[$P$BpyID37aTSEicSweMlC7ppaIo4E8ex0]]>
        </customer_password>
        <customer_password_salt>
            <![CDATA[]]>
        </customer_password_salt>
        <customer_password_hash_type>
            <![CDATA[phpass]]>
        </customer_password_hash_type>
        <customer_password_hash_config>
            <![CDATA[8]]>
        </customer_password_hash_config>
        <custom_fields>
        </custom_fields>
        <transaction_details>
            <transaction_detail>
                <product_name>
                    <![CDATA[11&quot; Extreme Sleeve™ for Laptops]]>
                </product_name>
                <product_price>
                    <![CDATA[69.95]]>
                </product_price>
                <product_quantity>
                    <![CDATA[1]]>
                </product_quantity>
                <product_weight>
                    <![CDATA[1.000]]>
                </product_weight>
                <product_code>
                    <![CDATA[0033]]>
                </product_code>
                <image>
                    <![CDATA[http://g-form.net/wp-content/uploads/2011/10/PRO-2285-18_large-150x150.jpg]]>
                </image>
                <url>
                    <![CDATA[http://g-form.net/products/11-extreme-sleeve%e2%84%a2-for-laptops/]]>
                </url>
                <length>
                    <![CDATA[0]]>
                </length>
                <width>
                    <![CDATA[0]]>
                </width>
                <height>
                    <![CDATA[0]]>
                </height>
                <downloadable_url>
                    <![CDATA[]]>
                </downloadable_url>
                <sub_token_url>
                    <![CDATA[]]>
                </sub_token_url>
                <subscription_frequency>
                    <![CDATA[]]>
                </subscription_frequency>
                <subscription_startdate>
                    <![CDATA[0000-00-00]]>
                </subscription_startdate>
                <subscription_nextdate>
                    <![CDATA[0000-00-00]]>
                </subscription_nextdate>
                <subscription_enddate>
                    <![CDATA[0000-00-00]]>
                </subscription_enddate>
                <is_future_line_item>
                    <![CDATA[0]]>
                </is_future_line_item>
                <shipto>
                    <![CDATA[]]>
                </shipto>
                <category_description>
                    <![CDATA[Default for all products]]>
                </category_description>
                <category_code>
                    <![CDATA[DEFAULT]]>
                </category_code>
                <product_delivery_type>
                    <![CDATA[notshipped]]>
                </product_delivery_type>
                <transaction_detail_options>
                    <transaction_detail_option>
                        <product_option_name>
                            <![CDATA[Color]]>
                        </product_option_name>
                        <product_option_value>
                            <![CDATA[Black]]>
                        </product_option_value>
                        <price_mod>
                            <![CDATA[0]]>
                        </price_mod>
                        <weight_mod>
                            <![CDATA[0.000]]>
                        </weight_mod>
                    </transaction_detail_option>
                </transaction_detail_options>
            </transaction_detail>
        </transaction_details>
        <shipto_addresses>
        </shipto_addresses>
        <attributes>
        </attributes>
    </transaction>
</transactions>
</foxydata>

看起来像这样。这是为SOAP调用格式化的:

<Order>
    <TransInfo>
        <ReferenceNum>AS7738</ReferenceNum>
        <EarliestShipDate>2008-02-11T00:00:00</EarliestShipDate>
        <ShipCancelDate>0001-01-01T00:00:00</ShipCancelDate>
    </TransInfo>
    <ShipTo>
        <CompanyName>BB &amp; B</CompanyName>
        <Address><Address1/><City/><State/><Zip/><Country/></Address>
    </ShipTo>
    <ShippingInstructions><Carrier>LVLA</Carrier></ShippingInstructions>
    <OrderLineItems>
        <OrderLineItem><SKU>12345</SKU><Qty>8.00000</Qty><Packed>2</Packed<CuFtPerCarton>1.400000</CuFtPerCarton></OrderLineItem>
        <OrderLineItem><SKU>98572</SKU><Qty>116.00000</Qty><Packed>4</Packed>CuFtPerCarton>2.100000</CuFtPerCarton></OrderLineItem>
        <OrderLineItem><SKU>98873</SKU><Qty>20.00000</Qty><Packed>4</Packed>CuFtPerCarton>2.100000</CuFtPerCarton></OrderLineItem>
        <OrderLineItem><SKU>36774</SKU><Qty>40.00000</Qty><Packed>4</Packed>CuFtPerCarton>2.100000</CuFtPerCarton></OrderLineItem>
    </OrderLineItems>
</Order>

这样做最好的方法是什么?

我提出的想法包括:

  1. 从foxycart Feed中提取必要的节点并将它们插入到SOAP数组中。

  2. 以某种方式过滤foxycart Feed,去除不必要的数据并根据需要修改节点。 (不知道我是如何实现这一目标的)。

  3. 有什么想法吗?

    谢谢, 雅各布

1 个答案:

答案 0 :(得分:1)

$data    = file_get_contents($xml);
$data    = new SimpleXMLElement($data);
$date    = date(DATE_ATOM);
$result  = $data->xpath('/foxydata/transactions/transaction');
$country = ( trim($result[0]->customer_country) == "US" ) ? "USA" : trim($result[0]->customer_country);
$i       = 0;

foreach ( $result[0]->transaction_details->transaction_detail as $transaction ) {

    $OrderLineItem['OrderLineItem'][$i] = array (
        'SKU'            => trim((string)$transaction->product_code),
        'Qty'            => trim((int)$transaction->product_quantity),
        'Packed'         => trim((string)$transaction->product_weight),
        'CuFtPerCarton'  => '0.0006'
    );
    $i++;

}



$ArrayOfOrder  = array ( 
    'Order' => array(
    'TransInfo'            => array(
        'ReferenceNum'      => trim((string)$result[0]->customer_id) . trim((string)$result[0]->id),
        'EarliestShipDate'  => $date,
        'ShipCancelDate'    => strtotime(date(DATE_ATOM, strtotime($date)) . "+1 month"),
        'PONum'             => trim((string)$result[0]->customer_last_name) . '_' . trim((string)$result[0]->id)
    ),
    'ShipTo'               => array(
        'Name'          => trim((string)$result[0]->customer_first_name) . ' ' . trim((string)$result[0]->customer_last_name),
        'CompanyName'   => trim((string)$result[0]->customer_company),
        'Address'       => array(
            'Address1' => trim((string)$result[0]->customer_address1),
            'Address2' => trim((string)$result[0]->customer_address2),
            'City'     => trim((string)$result[0]->customer_city),
            'State'    => trim((string)$result[0]->customer_state),
            'Zip'      => trim((string)$result[0]->customer_postal_code),
            'Country'  => $country
        ),
    'EmailAddress1' => trim((string)$result[0]->customer_email),
    'CustomerName'  => 'DEMO'

),
    'ShippingInstructions' => 'DEFAULT',
    'Notes'                => trim((string)$result[0]->processor_response),
    'PalletCount'          => 1,
    'OrderLineItems'       => $OrderLineItem
));