0

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.

IMSoP
99.4k18 gold badges135 silver badges182 bronze badges
asked Jun 27, 2014 at 15:47
4
  • 1
    Why are you trying to use JSON at all? Commented Jun 27, 2014 at 16:25
  • do a var_dump($xml); at the bottom - do you get anything? If so, please show it. Commented Jun 27, 2014 at 16:26
  • Please, remove the horrible "turn the SimpleXML object into JSON and then into an array" hack. SimpleXML has lots of useful accessors for dealing with XML; why waste all that to turn it into a much uglier array? It looks like you've checked what $response contains already. Show us that, and if the problem is below that point, we don't need to see where that data came from. Then perhaps try these debug functions to examine the $xml object, as var_dump can lie to you. Commented Jun 27, 2014 at 16:30
  • I'm willing to bet that your actual "problem" is that the XML contains namespaces (tags with colons in them); without seeing the content, I can't give a full example, but basically you need to use the ->children() and ->attributes() methods. And don't trust var_dump. Commented Jun 28, 2014 at 0:10

3 Answers 3

1

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>'.
answered Jun 27, 2014 at 16:26
0

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);
 } 
answered Jun 27, 2014 at 16:44
1
  • Obviously, if you're happy with it, fine. But I would really advise you to learn how to use SimpleXML rather than struggle with that awful function. The format returned is more like a dump of the parser loop than actual usable data, so you end up having to re-parse it into something useful. If you just show us what's in $response, we can show you how to use SimpleXML, and it will make your life much easier. Commented Jun 27, 2014 at 23:33
0

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;
answered Jun 28, 2014 at 12:11

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.