1

I'm trying to figure out how to get a specific value from the XML

Code :-

function getUrlContent($url,$xml_get_subscriber_info){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_get_subscriber_info);
$data = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
//return ($httpcode>=200 && $httpcode<300) ? $data : false;
return $data;

$a = simplexml_load_string(getURLContent($url,$xml_get_subscriber_info));

The XML :-

<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" ><SOAP-ENV:Body>
    <ns2:root>
        <msg_head>
            <time>2020-08-20 17:57:29</time>
            <from />
            <to />
            <msg_type />
            <serial />
        </msg_head>
        <interface_msg>
            <msg_response>
                <ResponseClass Name="Response">
                    <GetUserClass Name="AJAX">
                        <ResultCode>0</ResultCode>
                        <ResultDescr>success</ResultDescr>
                        <IBAN>1</IBAN>
                        <PACKAGEID>208$268$269$345$481$482$619$653$656$663$665$702$764$768$1128$1130$1143$1156$1166</PACKAGEID>
                        <USRSTATUS>1</USRSTATUS>
                        <PREFERNOTIFYMETHOD>1</PREFERNOTIFYMETHOD>
                    </GetUserClass>
                </ResponseClass>
            </msg_response>
        </interface_msg>
    </ns2:root>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

I'm trying to get the packageID payload but i've tried all sort even using loadXML function but sometimes i have parser issue using the loadXML due to high load but now i've decided to change to simple_load_xml.

I've tried the following method :-

print_r($xml->children("SOAP-ENV", true)->children("ns2", true));

Even tried to cast it as a namespace , no luck

$xml = simplexml_load_string($result,"SimpleXMLElement", 0, "SOAP-ENV", true);

1 Answer 1

1

Using DOMDocument() instead:

<?php

$doc = new DOMDocument();
$doc->loadXML($your_file_as_string);


$xpath = new DOMXPath($doc);

foreach ($xpath->query("//PACKAGEID/text()")  as $package) {
    print_r($package->textContent);
}
?>

This yields

208$268$269$345$481$482$619$653$656$663$665$702$764$768$1128$1130$1143$1156$1166                                                                                                                                 
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks @Jan for the feedback, instead of file as string can a curl response be used?
Also , this looks similar to what i've done . $result_provisioning = $result; $doc = new DOMDocument( '1.0', 'utf-8' ); $doc->loadXML($result_provisioning); $resultCode = $doc->getElementsByTagName('PackageID')->item(0)->nodeValue; But mine uses getElementBytagName and i'll hit the parser issue when i go above 50 tps
@FreedomPride: You did not specify this code in your question. And yes, a curl response can be used as long as it is a string.
I had to scrape the following code method because of the issue , but i'm trying your code.. After 50 tps, i'm observing intermittent error : DOMDocument::loadXML(): Empty string supplied as input in test.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.