user3771496
user3771496

Reputation: 27

Parsing XML file using php

Rephrase it. Here's my code together with the XML. Everytime I try to print_r the response, It does not reflect anything.

$portal   = 'CaregiverPortal';
$userName = 'dxt3uyk27U3wRRrzaFGiwQ==';
$password = 'wD81PILmPuJX2fyFek937A==';
$url      = "https://webapp.healthcaresynergy.com:8002/demoalpha/CaregiverPortalMobile/CaregiverPortalS   ervice.svc?singleWsdl";         
$option   = array('trace' => 1 ); 

$xml      = '<soapenv:Envelope   xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">'.
           '<soapenv:Header/>'.
           '<soapenv:Body>'.
              '<LoginCaregiverPortal>'.
                 '<userName>Anything</userName>'.
                 '<password>Anything</password>'.
                 '<portal>'.$portal.'</portal>'.
                 '<caregiverID>'.$userName.'</caregiverID>'.
                 '<timeStamp>'.$password.'</timeStamp>'.
              '</LoginCaregiverPortal>'.
           '</soapenv:Body>'.
         '</soapenv:Envelope>';

$client = new LocalSoapClient($url, $option, $xml);

try 
{ 
 $client->LoginCaregiverPortal();
 $response = $client->__getLastResponse(); 
 //echo 'result';
 //echo "<br/>";
 //echo htmlspecialchars($response);
 $xml = simplexml_load_string($response);
 $json = json_encode($xml);
 $array = json_decode($json,TRUE);
 print_r($array);

}

this is what I get as a result.

Array ( )

I think This is where I'm getting an empty result.

 $xml = simplexml_load_string($response);
 $json = json_encode($xml);
 $array = json_decode($json,TRUE);
 print_r($array);

Many thanks for the help.

Upvotes: 0

Views: 472

Answers (3)

IMSoP
IMSoP

Reputation: 97968

This JSON-encode-decode trick of turning the whole SimpleXML object into an array is entirely counter-productive, so firstly, drop that, and keep this bit:

 $client->LoginCaregiverPortal();
 $response = $client->__getLastResponse(); 
 $xml = simplexml_load_string($response);

What you now have in $xml is a SimpleXMLElement object. This has a lot of "magic" methods for letting you navigate through the XML, most of which are demonstrated on this page of the PHP manual.


Since this is a SOAP response, my guess is that the problem you're having is with "XML namespaces" - a tag name with a colon in, like <S:Envelope> is actually a tag called Envelope in the namespace with the local prefix of S. In SimpleXML, you have to "select" namespaces with ->children() and ->attributes().

You haven't shown the structure of the XML you got back, but assuming the sample in your other question is the result of running xml_parse_into_struct over it, it must look, in part, something like this:

<S:Envelope>
    <S:Body>
        <LoginCareGiverPortalResponse>
            <LoginCareGiverPortalResult>
                <A:Agencies>
                    <B:KeyValueOfIntHealthAgencyD9J3W_PIR>
                        <B:Value>
                        </B:Value>
                    </B:KeyValueOfIntHealthAgencyD9J3W_PIR>
                </A:Agencies>
                <A:Token>vy8BMS8nDIFdQWRTb6wyNDGGUMgBzHtOXU6mHqZgdxhRAbi0qkwluK9pjt03OQyf</A:Token>
            </LoginCareGiverPortalResult>
        </LoginCareGiverPortalResponse>
    </S:Body>
</S:Envelope>

To get the value of the A:Token tag (as mentioned on your other question), you would write this:

$token_element = $xml->children('S', true) // select the 'S' namespace
    ->Body                // $xml already points to <S:Envelope>
        ->children(null)  // select the namespace with no prefix
            ->LoginCareGiverPortalResponse
                 ->LoginCareGiverPortalResult
                     ->children('A', true) // select the 'A' namespace
                          Token;
// $token_element is an object pointing at that tag, we want its text content:
$token_text = (string)$token_element;

One caveat, though: the prefixes 'A' and 'B' have no universal meaning, and there is no guarantee that they won't get switched around in a different response. At the top of the response, you'll see attributes like xmlns:a="http://example.net/xml-namespaces/Thingummy" xmlns:b="http://example.net/xml-namespaces/Grommet"; those are the actual namespace identifiers, which won't change. xmlns:a is for everything beginning a:, and so on; the one just saying xmlns= is the "default namespace", for tags with no prefix like <LoginCareGiverPortalResponse>.

So if you look up what A, B, and the default are in your current example, you could write this:

// Define our own short-hands for the different namespaces
define('NS_SOAP', 'http://schemas.xmlsoap.org/soap/envelope/'); // This is standard
define('NS_RESPONSE', 'http://example.net/xml-namespaces/OurResponses');
define('NS_THINGUMMY', 'http://example.net/xml-namespaces/Thingummy');
define('NS_GROMMET', 'http://example.net/xml-namespaces/Grommet');

// Use them instead of the prefixes when selecting our namespaces
$token_element = $xml->children(NS_SOAP) // select the 'S' namespace
    ->Body                // $xml already points to <S:Envelope>
        ->children(NS_RESPONSE)  // select the "main" namespace
            ->LoginCareGiverPortalResponse
                 ->LoginCareGiverPortalResult
                     ->children(NS_THINGUMMY) // select the 'A' namespace
                          Token;

Upvotes: 0

user3771496
user3771496

Reputation: 27

Thanks for all the answers guys. It really helped me a lot to understand it.

However, I came up with a solution on nmy one which can and may help others as well if ever they encounter issues as such.

Here's what the try bracket in try catch was modified

  try 
{ 
 $client->LoginCaregiverPortal();
 $response = $client->__getLastResponse(); 
 //echo 'result';
 //echo "<br/>";
 //echo htmlspecialchars($response);
 $p        = xml_parser_create();
          xml_parse_into_struct($p, $response, $vals, $index);
          xml_parser_free($p);
          echo "Index array\n";
          echo('<pre>');
          print_r($index);
          echo('<pre/>');
          echo "\nVals array\n";
          print_r($vals);
     } 

Upvotes: 0

bhaskarc
bhaskarc

Reputation: 9541

While it is difficult to tell much, as you have not provided the code of LocalSoapClient that you have used in your program and there is not much known to me in terms of the requirements of healthcaresynergy.com.

However I see two probable errors. The two lines in your soap request should probably be:

'<userName>'.$userName.'</userName>'.
'<password>'.$password.'</password>'.

Upvotes: 1

Related Questions