简体   繁体   中英

XML Response - access certain fields

This is my XML response I need to access via PHP <CDer_ID>64846</CDer_ID> How do I single out that Number and use it as a variable?

Many thanks in advance..

    $value = get_field( "VehicleCode" );

                            $curl = curl_init();

                            curl_setopt_array($curl, array(
                            CURLOPT_URL => "https://soap.cap.co.uk/Vehicles/CapVehicles.asmx/GetCapidFromCapcode?subscriberId=***&password=**&database=CAR&caPcode=$value",
                            CURLOPT_RETURNTRANSFER => true,
                            CURLOPT_ENCODING => "",
                            CURLOPT_MAXREDIRS => 10,
                            CURLOPT_TIMEOUT => 30,
                            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
                            CURLOPT_CUSTOMREQUEST => "GET",
                            CURLOPT_HTTPHEADER => array(
                                "Accept: */*",
                                "Accept-Encoding: gzip, deflate",
                                "Cache-Control: no-cache",
                                "Connection: keep-alive",
                                "Host: soap.cap.co.uk",
                                "Postman-Token: d47f0fcb-93a6-4c0d-bf5f-7feb3ecbc2f3,662c8fc9-d1ff-4dfc-8063-0c281f78d2d8",
                                "User-Agent: PostmanRuntime/7.19.0",
                                "cache-control: no-cache"
                            ),
                            ));

                            $xml = curl_exec($curl);
                            $err = curl_error($curl);

                            curl_close($curl);

                            $dom=new DOMDocument;
                            $dom->loadXML( $xml ); # where $xml is the soap response XML string

                            $cder=$dom->getElementsByTagName('CDer_ID')[0];
                            echo $cder->nodeValue;


My Response


<?xml version="1.0" encoding="utf-8"?>
<CAPDataSetResult xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="https://soap.cap.co.uk/vehicles">
    <Success>true</Success>
    <FailMessage />
    <Returned_DataSet>
        <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
            <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
                <xs:complexType>
                    <xs:choice minOccurs="0" maxOccurs="unbounded">
                        <xs:element name="Table">
                            <xs:complexType>
                                <xs:sequence>
                                    <xs:element name="CDer_ID" type="xs:int" minOccurs="0" />
                                </xs:sequence>
                            </xs:complexType>
                        </xs:element>
                    </xs:choice>
                </xs:complexType>
            </xs:element>
        </xs:schema>
        <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
            <NewDataSet xmlns="">
                <Table diffgr:id="Table1" msdata:rowOrder="0">
                    <CDer_ID>64846</CDer_ID>
                </Table>
            </NewDataSet>
        </diffgr:diffgram>
    </Returned_DataSet>
</CAPDataSetResult>

In the simplest form to access the value of the tag CDer_ID you can do the following:

$dom=new DOMDocument;
$dom->loadXML( $xml ); # where $xml is the soap response XML string

$cder=$dom->getElementsByTagName('CDer_ID')[0];
echo $cder->nodeValue;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM