如何从curl读取XML响应

I have an XML string returned from the API and I'm having a hard time parsing it, not sure what I'm doing wrong.

I have tried

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

but I just get

SimpleXMLElement Object
(
)

I also tried $xml = new SimpleXMLElement($responseXml) but I can't get to the results.

Can anyone help or point me in the right direction? I would just need to get service_type, TOTAL_CHARGE and transit_time values.

Thanks in advance!

Here's the response I get:

<?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>
douyi7283
douyi7283 尝试$xml=simplexml_load_string(str_ireplace(“soapenv:”,“”,$responseXml));的print_r($XML);
3 年多之前 回复
dongwei1855
dongwei1855 如果您还没有这样做,请打开PHP错误。
3 年多之前 回复
dongwenhui8900
dongwenhui8900 如果您正在阅读SOAP,请使用SOAPapi,而不仅仅是XML。
3 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐