如何从curl读取XML响应

时间:2017-07-06 23:14:57

标签: php xml curl

我有一个从API返回的XML字符串,我很难解析它,不知道我做错了什么。

我试过了

$xml = simplexml_load_string($response);
print_r($xml);

但我得到了

SimpleXMLElement Object
(
)

我也试过$xml = new SimpleXMLElement($responseXml),但我无法得到结果。

任何人都可以帮助或指出我正确的方向吗?我只需要获取service_type,TOTAL_CHARGE和transit_time值。

提前致谢!

以下是我得到的回复:

<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope >
    <soapenv:Body>
        <ns:getRatesResponse xmlns:ns="http://ws.rating.uss.transforce.ca">
            <ns:return xmlns:ax29="http://ws.rating.uss.transforce.ca/xsd" xmlns:ax211="http://dto.uss.transforce.ca/xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ax29:GetRatesRs">
                <ax29:error xsi:nil="true" />
                <ax29:getRatesResult xsi:type="ax211:GetRatesResult">
                    <ax211:shipment xsi:type="ax211:Shipment">
                        <ax211:billed_weight>6.0</ax211:billed_weight>
                        <ax211:billed_weight_unit>L</ax211:billed_weight_unit>
                        <ax211:collect_shipper_num></ax211:collect_shipper_num>
                        <ax211:consolidation_type></ax211:consolidation_type>
                        <ax211:courier>L</ax211:courier>
                        <ax211:delivery_address_id></ax211:delivery_address_id>
                        <ax211:delivery_address_line_1>XYZ</ax211:delivery_address_line_1>
                        <ax211:delivery_address_line_2></ax211:delivery_address_line_2>
                        <ax211:delivery_address_line_3></ax211:delivery_address_line_3>
                        <ax211:delivery_city>LONDON</ax211:delivery_city>
                        <ax211:delivery_country>CA</ax211:delivery_country>
                        <ax211:delivery_email></ax211:delivery_email>
                        <ax211:delivery_extension></ax211:delivery_extension>
                        <ax211:delivery_name>XYZ</ax211:delivery_name>
                        <ax211:delivery_phone></ax211:delivery_phone>
                        <ax211:delivery_postal_code>123456</ax211:delivery_postal_code>
                        <ax211:delivery_province>ON</ax211:delivery_province>
                        <ax211:delivery_residential>false</ax211:delivery_residential>
                        <ax211:dimension_unit>I</ax211:dimension_unit>
                        <ax211:estimated_delivery_date xsi:nil="true" />
                        <ax211:freight_charge>0.00</ax211:freight_charge>
                        <ax211:fuel_surcharge>1.04</ax211:fuel_surcharge>
                        <ax211:id>-1</ax211:id>
                        <ax211:inserted_on>2017-07-06T18:18:30.201-04:00</ax211:inserted_on>
                        <ax211:manifest_num xsi:nil="true" />
                        <ax211:packages xsi:type="ax211:Package">
                            <ax211:billed_weight>5.8</ax211:billed_weight>
                            <ax211:dim_weight>0.0</ax211:dim_weight>
                            <ax211:dim_weight_flag>false</ax211:dim_weight_flag>
                            <ax211:id>-1</ax211:id>
                            <ax211:inserted_on>2017-07-06T18:18:30.201-04:00</ax211:inserted_on>
                            <ax211:min_weight_flag>false</ax211:min_weight_flag>
                            <ax211:package_info_num xsi:type="ax211:PackageInfoNum">
                                <ax211:id>-1</ax211:id>
                                <ax211:inserted_on>2017-07-06T18:18:30.201-04:00</ax211:inserted_on>
                                <ax211:name>LENGTH</ax211:name>
                                <ax211:updated_on>2017-07-06T18:18:30.201-04:00</ax211:updated_on>
                                <ax211:value>0.00</ax211:value>
                            </ax211:package_info_num>
                            <ax211:package_info_num xsi:type="ax211:PackageInfoNum">
                                <ax211:id>-1</ax211:id>
                                <ax211:inserted_on>2017-07-06T18:18:30.201-04:00</ax211:inserted_on>
                                <ax211:name>WIDTH</ax211:name>
                                <ax211:updated_on>2017-07-06T18:18:30.201-04:00</ax211:updated_on>
                                <ax211:value>0.00</ax211:value>
                            </ax211:package_info_num>
                            <ax211:package_info_num xsi:type="ax211:PackageInfoNum">
                                <ax211:id>-1</ax211:id>
                                <ax211:inserted_on>2017-07-06T18:18:30.201-04:00</ax211:inserted_on>
                                <ax211:name>HEIGHT</ax211:name>
                                <ax211:updated_on>2017-07-06T18:18:30.201-04:00</ax211:updated_on>
                                <ax211:value>0.00</ax211:value>
                            </ax211:package_info_num>
                            <ax211:package_num>0</ax211:package_num>
                            <ax211:package_reference>0</ax211:package_reference>
                            <ax211:reported_weight>5.8</ax211:reported_weight>
                            <ax211:updated_on>2017-07-06T18:18:30.201-04:00</ax211:updated_on>
                        </ax211:packages>
                        <ax211:pickup_address_line_1>XYZ</ax211:pickup_address_line_1>
                        <ax211:pickup_address_line_2></ax211:pickup_address_line_2>
                        <ax211:pickup_address_line_3></ax211:pickup_address_line_3>
                        <ax211:pickup_city>XYZ</ax211:pickup_city>
                        <ax211:pickup_email></ax211:pickup_email>
                        <ax211:pickup_extension></ax211:pickup_extension>
                        <ax211:pickup_name>XYZ</ax211:pickup_name>
                        <ax211:pickup_phone></ax211:pickup_phone>
                        <ax211:pickup_postal_code>654321</ax211:pickup_postal_code>
                        <ax211:pickup_province>ON</ax211:pickup_province>
                        <ax211:proforma xsi:nil="true" />
                        <ax211:reported_weight_unit>L</ax211:reported_weight_unit>
                        <ax211:service_type>DD</ax211:service_type>
                        <ax211:shipment_info_num xsi:type="ax211:ShipmentInfoNum">
                            <ax211:id>-1</ax211:id>
                            <ax211:inserted_on>2017-07-06T18:18:30.201-04:00</ax211:inserted_on>
                            <ax211:name>DECLARED_VALUE</ax211:name>
                            <ax211:updated_on>2017-07-06T18:18:30.201-04:00</ax211:updated_on>
                            <ax211:value>0.00</ax211:value>
                        </ax211:shipment_info_num>
                        <ax211:shipment_info_num xsi:type="ax211:ShipmentInfoNum">
                            <ax211:id>-1</ax211:id>
                            <ax211:inserted_on>2017-07-06T18:18:30.201-04:00</ax211:inserted_on>
                            <ax211:name>BASE_CHARGE</ax211:name>
                            <ax211:updated_on>2017-07-06T18:18:30.201-04:00</ax211:updated_on>
                            <ax211:value>5.63</ax211:value>
                        </ax211:shipment_info_num>
                        <ax211:shipment_info_num xsi:type="ax211:ShipmentInfoNum">
                            <ax211:id>-1</ax211:id>
                            <ax211:inserted_on>2017-07-06T18:18:30.201-04:00</ax211:inserted_on>
                            <ax211:name>TOTAL_CHARGE</ax211:name>
                            <ax211:updated_on>2017-07-06T18:18:30.201-04:00</ax211:updated_on>
                            <ax211:value>7.54</ax211:value>
                        </ax211:shipment_info_num>
                        <ax211:shipment_info_num xsi:type="ax211:ShipmentInfoNum">
                            <ax211:id>-1</ax211:id>
                            <ax211:inserted_on>2017-07-06T18:18:30.201-04:00</ax211:inserted_on>
                            <ax211:name>CUBIC_FACTOR</ax211:name>
                            <ax211:updated_on>2017-07-06T18:18:30.201-04:00</ax211:updated_on>
                            <ax211:value>15.00</ax211:value>
                        </ax211:shipment_info_num>
                        <ax211:shipment_info_str xsi:type="ax211:ShipmentInfoStr">
                            <ax211:id>-1</ax211:id>
                            <ax211:inserted_on>2017-07-06T18:18:30.201-04:00</ax211:inserted_on>
                            <ax211:name>FRAGILE</ax211:name>
                            <ax211:updated_on>2017-07-06T18:18:30.201-04:00</ax211:updated_on>
                            <ax211:value>FALSE</ax211:value>
                        </ax211:shipment_info_str>
                        <ax211:shipment_info_str xsi:type="ax211:ShipmentInfoStr">
                            <ax211:id>-1</ax211:id>
                            <ax211:inserted_on>2017-07-06T18:18:30.201-04:00</ax211:inserted_on>
                            <ax211:name>DANGEROUS_GOODS</ax211:name>
                            <ax211:updated_on>2017-07-06T18:18:30.201-04:00</ax211:updated_on>
                            <ax211:value>FALSE</ax211:value>
                        </ax211:shipment_info_str>
                        <ax211:shipment_info_str xsi:type="ax211:ShipmentInfoStr">
                            <ax211:id>-1</ax211:id>
                            <ax211:inserted_on>2017-07-06T18:18:30.201-04:00</ax211:inserted_on>
                            <ax211:name>SAT_DELIVERY</ax211:name>
                            <ax211:updated_on>2017-07-06T18:18:30.201-04:00</ax211:updated_on>
                            <ax211:value>FALSE</ax211:value>
                        </ax211:shipment_info_str>
                        <ax211:shipment_info_str xsi:type="ax211:ShipmentInfoStr">
                            <ax211:id>-1</ax211:id>
                            <ax211:inserted_on>2017-07-06T18:18:30.201-04:00</ax211:inserted_on>
                            <ax211:name>CODE</ax211:name>
                            <ax211:updated_on>2017-07-06T18:18:30.201-04:00</ax211:updated_on>
                            <ax211:value>AA</ax211:value>
                        </ax211:shipment_info_str>
                        <ax211:shipment_info_str xsi:type="ax211:ShipmentInfoStr">
                            <ax211:id>-1</ax211:id>
                            <ax211:inserted_on>2017-07-06T18:18:30.201-04:00</ax211:inserted_on>
                            <ax211:name>SERVICE_LABEL</ax211:name>
                            <ax211:updated_on>2017-07-06T18:18:30.201-04:00</ax211:updated_on>
                            <ax211:value>GRD</ax211:value>
                        </ax211:shipment_info_str>
                        <ax211:shipment_info_str xsi:type="ax211:ShipmentInfoStr">
                            <ax211:id>-1</ax211:id>
                            <ax211:inserted_on>2017-07-06T18:18:30.201-04:00</ax211:inserted_on>
                            <ax211:name>BRANCH_CITY</ax211:name>
                            <ax211:updated_on>2017-07-06T18:18:30.201-04:00</ax211:updated_on>
                            <ax211:value>LONDON</ax211:value>
                        </ax211:shipment_info_str>
                        <ax211:shipment_status>R</ax211:shipment_status>
                        <ax211:shipper_num>123123</ax211:shipper_num>
                        <ax211:shipping_date>20170707</ax211:shipping_date>
                        <ax211:tax_charge_1>0.87</ax211:tax_charge_1>
                        <ax211:tax_charge_2>0.00</ax211:tax_charge_2>
                        <ax211:tax_code_1>GST</ax211:tax_code_1>
                        <ax211:tax_code_2></ax211:tax_code_2>
                        <ax211:transit_time>1</ax211:transit_time>
                        <ax211:transit_time_guaranteed>false</ax211:transit_time_guaranteed>
                        <ax211:updated_on>2017-07-06T18:18:30.201-04:00</ax211:updated_on>
                        <ax211:user_id>OUR_USER_ID</ax211:user_id>
                        <ax211:voided>false</ax211:voided>
                        <ax211:zone>2</ax211:zone>
                    </ax211:shipment>
                </ax29:getRatesResult>
                <ax29:getRatesResult xsi:type="ax211:GetRatesResult">
                    <ax211:shipment xsi:type="ax211:Shipment">
                        <ax211:billed_weight>6.0</ax211:billed_weight>
                        <ax211:billed_weight_unit>L</ax211:billed_weight_unit>
                        <ax211:collect_shipper_num></ax211:collect_shipper_num>
                        <ax211:consolidation_type></ax211:consolidation_type>
                        <ax211:courier>L</ax211:courier>
                        <ax211:delivery_address_id></ax211:delivery_address_id>
                        <ax211:delivery_address_line_1>XYZ</ax211:delivery_address_line_1>
                        <ax211:delivery_address_line_2></ax211:delivery_address_line_2>
                        <ax211:delivery_address_line_3></ax211:delivery_address_line_3>
                        <ax211:delivery_city>LONDON</ax211:delivery_city>
                        <ax211:delivery_country>CA</ax211:delivery_country>
                        <ax211:delivery_email></ax211:delivery_email>
                        <ax211:delivery_extension></ax211:delivery_extension>
                        <ax211:delivery_name>XYZ</ax211:delivery_name>
                        <ax211:delivery_phone></ax211:delivery_phone>
                        <ax211:delivery_postal_code>123456</ax211:delivery_postal_code>
                        <ax211:delivery_province>ON</ax211:delivery_province>
                        <ax211:delivery_residential>false</ax211:delivery_residential>
                        <ax211:dimension_unit>I</ax211:dimension_unit>
                        <ax211:estimated_delivery_date xsi:nil="true" />
                        <ax211:freight_charge>0.00</ax211:freight_charge>
                        <ax211:fuel_surcharge>2.48</ax211:fuel_surcharge>
                        <ax211:id>-1</ax211:id>
                        <ax211:inserted_on>2017-07-06T18:18:30.201-04:00</ax211:inserted_on>
                        <ax211:manifest_num xsi:nil="true" />
                        <ax211:packages xsi:type="ax211:Package">
                            <ax211:billed_weight>5.8</ax211:billed_weight>
                            <ax211:dim_weight>0.0</ax211:dim_weight>
                            <ax211:dim_weight_flag>false</ax211:dim_weight_flag>
                            <ax211:id>-1</ax211:id>
                            <ax211:inserted_on>2017-07-06T18:18:30.201-04:00</ax211:inserted_on>
                            <ax211:min_weight_flag>false</ax211:min_weight_flag>
                            <ax211:package_info_num xsi:type="ax211:PackageInfoNum">
                                <ax211:id>-1</ax211:id>
                                <ax211:inserted_on>2017-07-06T18:18:30.201-04:00</ax211:inserted_on>
                                <ax211:name>LENGTH</ax211:name>
                                <ax211:updated_on>2017-07-06T18:18:30.201-04:00</ax211:updated_on>
                                <ax211:value>0.00</ax211:value>
                            </ax211:package_info_num>
                            <ax211:package_info_num xsi:type="ax211:PackageInfoNum">
                                <ax211:id>-1</ax211:id>
                                <ax211:inserted_on>2017-07-06T18:18:30.201-04:00</ax211:inserted_on>
                                <ax211:name>WIDTH</ax211:name>
                                <ax211:updated_on>2017-07-06T18:18:30.201-04:00</ax211:updated_on>
                                <ax211:value>0.00</ax211:value>
                            </ax211:package_info_num>
                            <ax211:package_info_num xsi:type="ax211:PackageInfoNum">
                                <ax211:id>-1</ax211:id>
                                <ax211:inserted_on>2017-07-06T18:18:30.201-04:00</ax211:inserted_on>
                                <ax211:name>HEIGHT</ax211:name>
                                <ax211:updated_on>2017-07-06T18:18:30.201-04:00</ax211:updated_on>
                                <ax211:value>0.00</ax211:value>
                            </ax211:package_info_num>
                            <ax211:package_num>0</ax211:package_num>
                            <ax211:package_reference>0</ax211:package_reference>
                            <ax211:reported_weight>5.8</ax211:reported_weight>
                            <ax211:updated_on>2017-07-06T18:18:30.201-04:00</ax211:updated_on>
                        </ax211:packages>
                        <ax211:pickup_address_line_1>XYZ</ax211:pickup_address_line_1>
                        <ax211:pickup_address_line_2></ax211:pickup_address_line_2>
                        <ax211:pickup_address_line_3></ax211:pickup_address_line_3>
                        <ax211:pickup_city>XYZ</ax211:pickup_city>
                        <ax211:pickup_email></ax211:pickup_email>
                        <ax211:pickup_extension></ax211:pickup_extension>
                        <ax211:pickup_name>XYZ</ax211:pickup_name>
                        <ax211:pickup_phone></ax211:pickup_phone>
                        <ax211:pickup_postal_code>654321</ax211:pickup_postal_code>
                        <ax211:pickup_province>ON</ax211:pickup_province>
                        <ax211:proforma xsi:nil="true" />
                        <ax211:reported_weight_unit>L</ax211:reported_weight_unit>
                        <ax211:service_type>D9</ax211:service_type>
                        <ax211:shipment_info_num xsi:type="ax211:ShipmentInfoNum">
                            <ax211:id>-1</ax211:id>
                            <ax211:inserted_on>2017-07-06T18:18:30.201-04:00</ax211:inserted_on>
                            <ax211:name>DECLARED_VALUE</ax211:name>
                            <ax211:updated_on>2017-07-06T18:18:30.201-04:00</ax211:updated_on>
                            <ax211:value>0.00</ax211:value>
                        </ax211:shipment_info_num>
                        <ax211:shipment_info_num xsi:type="ax211:ShipmentInfoNum">
                            <ax211:id>-1</ax211:id>
                            <ax211:inserted_on>2017-07-06T18:18:30.201-04:00</ax211:inserted_on>
                            <ax211:name>BASE_CHARGE</ax211:name>
                            <ax211:updated_on>2017-07-06T18:18:30.201-04:00</ax211:updated_on>
                            <ax211:value>13.40</ax211:value>
                        </ax211:shipment_info_num>
                        <ax211:shipment_info_num xsi:type="ax211:ShipmentInfoNum">
                            <ax211:id>-1</ax211:id>
                            <ax211:inserted_on>2017-07-06T18:18:30.201-04:00</ax211:inserted_on>
                            <ax211:name>TOTAL_CHARGE</ax211:name>
                            <ax211:updated_on>2017-07-06T18:18:30.201-04:00</ax211:updated_on>
                            <ax211:value>17.94</ax211:value>
                        </ax211:shipment_info_num>
                        <ax211:shipment_info_num xsi:type="ax211:ShipmentInfoNum">
                            <ax211:id>-1</ax211:id>
                            <ax211:inserted_on>2017-07-06T18:18:30.201-04:00</ax211:inserted_on>
                            <ax211:name>CUBIC_FACTOR</ax211:name>
                            <ax211:updated_on>2017-07-06T18:18:30.201-04:00</ax211:updated_on>
                            <ax211:value>15.00</ax211:value>
                        </ax211:shipment_info_num>
                        <ax211:shipment_info_str xsi:type="ax211:ShipmentInfoStr">
                            <ax211:id>-1</ax211:id>
                            <ax211:inserted_on>2017-07-06T18:18:30.201-04:00</ax211:inserted_on>
                            <ax211:name>FRAGILE</ax211:name>
                            <ax211:updated_on>2017-07-06T18:18:30.201-04:00</ax211:updated_on>
                            <ax211:value>FALSE</ax211:value>
                        </ax211:shipment_info_str>
                        <ax211:shipment_info_str xsi:type="ax211:ShipmentInfoStr">
                            <ax211:id>-1</ax211:id>
                            <ax211:inserted_on>2017-07-06T18:18:30.201-04:00</ax211:inserted_on>
                            <ax211:name>DANGEROUS_GOODS</ax211:name>
                            <ax211:updated_on>2017-07-06T18:18:30.201-04:00</ax211:updated_on>
                            <ax211:value>FALSE</ax211:value>
                        </ax211:shipment_info_str>
                        <ax211:shipment_info_str xsi:type="ax211:ShipmentInfoStr">
                            <ax211:id>-1</ax211:id>
                            <ax211:inserted_on>2017-07-06T18:18:30.201-04:00</ax211:inserted_on>
                            <ax211:name>SAT_DELIVERY</ax211:name>
                            <ax211:updated_on>2017-07-06T18:18:30.201-04:00</ax211:updated_on>
                            <ax211:value>FALSE</ax211:value>
                        </ax211:shipment_info_str>
                        <ax211:shipment_info_str xsi:type="ax211:ShipmentInfoStr">
                            <ax211:id>-1</ax211:id>
                            <ax211:inserted_on>2017-07-06T18:18:30.201-04:00</ax211:inserted_on>
                            <ax211:name>CODE</ax211:name>
                            <ax211:updated_on>2017-07-06T18:18:30.201-04:00</ax211:updated_on>
                            <ax211:value>48</ax211:value>
                        </ax211:shipment_info_str>
                        <ax211:shipment_info_str xsi:type="ax211:ShipmentInfoStr">
                            <ax211:id>-1</ax211:id>
                            <ax211:inserted_on>2017-07-06T18:18:30.201-04:00</ax211:inserted_on>
                            <ax211:name>SERVICE_LABEL</ax211:name>
                            <ax211:updated_on>2017-07-06T18:18:30.201-04:00</ax211:updated_on>
                            <ax211:value>EXG900</ax211:value>
                        </ax211:shipment_info_str>
                        <ax211:shipment_info_str xsi:type="ax211:ShipmentInfoStr">
                            <ax211:id>-1</ax211:id>
                            <ax211:inserted_on>2017-07-06T18:18:30.201-04:00</ax211:inserted_on>
                            <ax211:name>BRANCH_CITY</ax211:name>
                            <ax211:updated_on>2017-07-06T18:18:30.201-04:00</ax211:updated_on>
                            <ax211:value>LONDON</ax211:value>
                        </ax211:shipment_info_str>
                        <ax211:shipment_status>R</ax211:shipment_status>
                        <ax211:shipper_num>123123</ax211:shipper_num>
                        <ax211:shipping_date>20170707</ax211:shipping_date>
                        <ax211:tax_charge_1>2.06</ax211:tax_charge_1>
                        <ax211:tax_charge_2>0.00</ax211:tax_charge_2>
                        <ax211:tax_code_1>GST</ax211:tax_code_1>
                        <ax211:tax_code_2></ax211:tax_code_2>
                        <ax211:transit_time>1</ax211:transit_time>
                        <ax211:transit_time_guaranteed>false</ax211:transit_time_guaranteed>
                        <ax211:updated_on>2017-07-06T18:18:30.201-04:00</ax211:updated_on>
                        <ax211:user_id>OUR_USER_ID</ax211:user_id>
                        <ax211:voided>false</ax211:voided>
                        <ax211:zone>2</ax211:zone>
                    </ax211:shipment>
                </ax29:getRatesResult>
            </ns:return>
        </ns:getRatesResponse>
    </soapenv:Body>
</soapenv:Envelope>

0 个答案:

没有答案