Chris Hampton
Chris Hampton

Reputation: 41

Consuming SOAP webservice in PHP

I am trying to build a client to consume a webservice, and have run into some strange issues. Here is my code:

$securityCode = "A7D5B7D8-73E2-44D2-A6F8-4ACFB91843BF"; // The security code has been changed to an invalid code to prevent unwanted "visitors".
$ProphecyConnect = new SoapClient("http://test.prophecyhealth.com/ProphecyConnect/ProphecyConnectXML.cfc?wsdl");
try
{
    $params = array(SecurityCode => $securityCode, AssessmentID => -1, AssessmentType => "Test");
    $assessmentList = $ProphecyConnect->__soapCall("GetAssessments", array($params));
}
catch(Exception $exception)
{
    var_dump($exception);
}
$xml = new DOMDocument();
$xml->loadXML( $assessmentList );
try
{
    foreach($xml->getElementsByTagName("assessment") as $assessment)
    {
        foreach($assessment->childNodes as $node)
        {
            printf(
            "Name: %s - Type: %s - Value: %s\n",
            $node->nodeName,
            $node->nodeType,
            urlencode($node->nodeValue)     
            );
        }
    }
}
catch(Exception $ex)
{
    echo "Something happened.";
    var_dump($ex);
}

My problem is that the getElementByTagName never finds anything. This is the returned XML from the webservice:

<object>
    <success>true</success>
    <count>3</count>
    <assessments>
        <assessment>
            <assessmentid><![CDATA[123]]></assessmentid>
            <assessmentname><![CDATA[Cardiac Cath Lab V1]]></assessmentname>
            <assessmenttype><![CDATA[Test]]></assessmenttype>
            <costpoints><![CDATA[1]]></costpoints>
            <numberofquestions><![CDATA[23]]></numberofquestions>
            <timelimit><![CDATA[1380]]></timelimit>
        </assessment>
        <assessment>
            <assessmentid><![CDATA[456]]></assessmentid>
            <assessmentname><![CDATA[Cardiac Progressive Care Exam A V1]]></assessmentname>
            <assessmenttype><![CDATA[Test]]></assessmenttype>
            <costpoints><![CDATA[1]]></costpoints>
            <numberofquestions><![CDATA[75]]></numberofquestions>
            <timelimit><![CDATA[4500]]></timelimit>
        </assessment>
        <assessment>
            <assessmentid><![CDATA[789]]></assessmentid>
            <assessmentname><![CDATA[Cardiac Progressive Care Exam B V1]]></assessmentname>
            <assessmenttype><![CDATA[Test]]></assessmenttype>
            <costpoints><![CDATA[1]]></costpoints>
            <numberofquestions><![CDATA[75]]></numberofquestions>
            <timelimit><![CDATA[4500]]></timelimit>
        </assessment>
    </assessments>
</object>

I'm quite the n00b when it comes to PHP, but as far as I can tell, this looks right (at least close). I'm sure I'm missing something blatently obvious though.

Thanks

Upvotes: 1

Views: 2374

Answers (1)

Chris Hampton
Chris Hampton

Reputation: 41

Turns out that the SOAP calls returns an object, instead of a string. So when I was passing the $assessmentList variable to the $xml.loadXML() function, I actually needed to pass the property that contained the xml. Like thus:

$xml->loadXML($assessmentList->GetAssessmentsReturn);

This worked perfectly.

Upvotes: 1

Related Questions