0

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>
  • 2
    You haven't shared a single bit of PHP code. What exact part of the SOAP handling routine do you need help with? – Álvaro González Nov 01 '19 at 09:29
  • Apologies weren't sure if it was necessary for me to share the Call, I have shared the call above. – Dillon Jenkins Nov 01 '19 at 09:33
  • Sorry, I was confused by the [tag:soap] tag. I thought you were using [SOAP](https://www.php.net/soap). You're basically just asking how to parse XML in general terms, right? – Álvaro González Nov 01 '19 at 09:36
  • I think it is a duplicate: See https://stackoverflow.com/questions/21777075/how-to-convert-soap-response-to-php-array some thing like this should work for you: ($xmlString = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $xmlString); $xml = new SimpleXMLElement($xmlString); var_dump($xml);) – Ali Khalili Nov 01 '19 at 09:37
  • Yes basically. Thank you for your response. – Dillon Jenkins Nov 01 '19 at 09:38

1 Answers1

2

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;
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46