0

I am using a 3rd party web service to post the request and get the response via soap using php curl.The response is fine but for some reason I can't parse the response.When I use simplexml_load_string it gives me blank.

My code is

// PHP cURL  for https connection with auth

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $soapUser.":".$soapPassword); // username and password - declared at the top of the doc
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // the SOAP request
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

// converting
$response = curl_exec($ch);              
print_r($response); // when I print response it shows me the correct response with data.
but when I am doing ,

$parser = simplexml_load_string($response2);         
print_r($parser); // it's giving SimpleXMLElement Object ( ) 

The header is

$headers = array(
                        "Content-type: text/xml;charset=\"utf-8\"",
                        "Accept: application/xml",
                        "Cache-Control: no-cache",
                        "Pragma: no-cache",

                        "Content-length: ".strlen($xml_post_string),
                    );  

The soap header is

<?xml version="1.0" encoding="utf-8"?>
                            <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.mrted.com/">

   <soapenv:Header>

      <wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">

         <wsse:UsernameToken wsu:Id="UsernameToken-1">

            <wsse:Username>*********************:guest:FO</wsse:Username>

            <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">guest</wsse:Password>

            <wsu:Created>2012-07-09T11:35:20.019Z</wsu:Created>

         </wsse:UsernameToken>

      </wsse:Security>

   </soapenv:Header>

The response which I am getting is after printing curl response in browser --

 <env:envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
    <env:header></env:header>
    <env:body>
    <ns2:getcriteriaresponse xmlns:ns2="http://ws.mrted.com/">
    <ns2:standardcriteriawithlovs>
    <adlanguages><language><label>English (UK)</label><value>UK</value></language></adlanguages>
 <countries><country><label>Australia</label><value>1116</value><regions><region><label>Australian Capital Territory</label><value>3027</value></region><region><label>New South Wales</label><value>3028</value></region><region><label>Northern Territory</label><value>3029</value></region><region><label>Queensland</label><value>3030</value></region><region><label>South Australia</label><value>3031</value></region><region><label>Tasmania</label><value>3032</value></region><region><label>Victoria</label><value>3033</value></region><region><label>Western Australia</label><value>3034</value></region></regions></country><country><label>United Kingdom</label><value>1290</value><regions><region><label>East Anglia</label><value>3429</value></region><region><label>East Midlands</label><value>3430</value></region><region><label>England</label><value>4329</value></region><region><label>London</label><value>3431</value></region><region><label>Midlands</label><value>3432</value></region><region><label>North East</label><value>3433</value></region><region><label>North West</label><value>3434</value></region><region><label>Northern Ireland</label><value>3435</value></region><region><label>Scotland</label><value>3436</value></region><region><label>South</label><value>4332</value></region><region><label>South East</label><value>3437</value></region><region><label>South West</label><value>3438</value></region><region><label>Wales</label><value>3439</value></region><region><label>West Midlands</label><value>3440</value></region></regions></country></countries>
    </ns2:standardcriteriawithlovs>
    </ns2:getcriteriaresponse>
    </env:body>
    </env:envelope>

It is sure that, I am getting the data but the format might be wrong or I am missing something.

Please suggest.Many thanks in advance.

7
  • 1
    That's because SimpleXML doesn't return an array, it returns nested collection of SimpleXML Element Objects; you need to load it and then parse it to locate what you need, which can then be cast to string/integer/float/etc Commented Aug 29, 2013 at 11:13
  • Thanks Mark.Do you have any reference manual how to load and then parse please. Commented Aug 29, 2013 at 11:47
  • 1
    php.net/manual/en/simplexml.examples-basic.php is a good page to start Commented Aug 29, 2013 at 11:57
  • Thanks for the link.I checked it. The document tree starts with the below thing. <env:Envelope><env:Header/><env:Body><ns2:getCriteriaResponse><ns2:standardCriteriaWithLovs><adLanguages><language><label>English (UK)</label><value>UK</value></language></adLanguages>......so on.I am bit confused where to start from I mean after doing $response = curl_exec($ch); $res = new SimpleXMLElement($response); how can I fetch the adlanguages node. Commented Aug 29, 2013 at 12:41
  • 1
    Awkward because that doesn't look like the full xml, as there's no namespace definitions: can you edit the full xml (exactly as you get it from the curl call) into your question? Commented Aug 29, 2013 at 13:15

1 Answer 1

3
$response2 = <<<EOF
<env:envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
    <env:header></env:header>
    <env:body>
        <ns2:getcriteriaresponse xmlns:ns2="http://ws.mrted.com/">
            <ns2:standardcriteriawithlovs>
                <adlanguages>
                    <language>
                        <label>English (UK)</label>
                        <value>UK</value>
                    </language>
                </adlanguages>
            </ns2:standardcriteriawithlovs>
        </ns2:getcriteriaresponse>
    </env:body>
</env:envelope>
EOF;

$parser = simplexml_load_string($response2);  
$parserEnv = $parser->children('env', true);
$languages = $parserEnv->body->children('ns2', true)
    ->getcriteriaresponse->children('ns2', true)
    ->standardcriteriawithlovs->children()
    ->adlanguages->children();

foreach($languages as $language) {
    $label = (string) $language->label;
    $value = (string) $language->value;
    echo $label, ' => ', $value, PHP_EOL;
}
Sign up to request clarification or add additional context in comments.

6 Comments

From now on you are my guru.It's just brilliant piece of coding.Thank you so much.One last thing that is if I have the countries as children of 'standardcriteriawithlovs' and under countries I have got two countries and I want to select the second country's all of the regions(the response is in my post) then will it be $languages = $parserEnv->Body->children('ns2', true) ->getCriteriaResponse->children('ns2', true) ->standardCriteriaWithLovs->children() ->countries->children()->country[1]->children(); ?
I'd loop through the standardcriteriawithlovs->children() using the getName() method to test if each child was an adlanguage or a country, then looping through each of those children's children to get the individual language or country details; but you could use your direct approach as long as you know the countries will always be there
let me try with the getName() method.
Solved...I have used getName to identify the country and then again foreach loop to get into it's children nodes....it's absolutely lovely experience.Thank you so much for this.
Namespaces are a painful learning experience with SimpleXML, but once you've got your head round them they're fairly straightforward
|

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.