Solved

PHP Parse SOAP Data

Posted on 2010-09-14
13
547 Views
Last Modified: 2012-05-10
Hello, I am having trouble parsing returned data from a soap call. I have attached a text file which is a print_r of the returned data.

I need to get the objects name, description, etc., and I am not able to. Please help!!!

Prasad
example.txt
0
Comment
Question by:iameye
  • 7
  • 4
  • 2
13 Comments
 
LVL 5

Expert Comment

by:sscotti
ID: 33674369
0
 
LVL 5

Expert Comment

by:sscotti
ID: 33674413
You can also look at the discussions here:

http://www.webmasterworld.com/php/3615040.htm
0
 

Author Comment

by:iameye
ID: 33677016
Yes, these seem to deal with xml, not the stdobject array structure I posted in the file. I had already tried some of those methods anyway, like the first link that you sent just to see and most of them returned errors that the data could not be converted to a string. And yes, I tried explicitly turning it into a string.

I need someone to actually take a look, run some code against the file, see if it works, then let me know, although I am willing to try stuff people suggest to me (not tested) as well if necessary. The former is much better, however, of course!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:iameye
ID: 33686195
I am increasing the point value as I need this answer as quickly as possible!
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 33691070
I may be able to help if I can see the code you use to call the SOAP service.  I would need to be able to test it.  Please post that back here so I can access the service with my own script and I will show you what we can do with the returned information.

You might also want to contact the publisher of this information and ask if they have anything easier to use than SOAP.  Many publishers use REST calls that return XML or JSON -- much easier to use that kind of data!
0
 

Author Comment

by:iameye
ID: 33694480
Okay, here is the code I use to call...

// submit xml to benefits calculator
      // returnFormat: 1->array, 2->html, 3->xml, 4->pdf
      $params = array(
        'xmlInput' => $xml_string,
        'returnFormat' => 1
    );
      
      $client = new SoapClient(
        "http://labs.gosmcpartners.com/CAFCA_BenefitsCalculator_WebService/BenefitsCalculator_WebService.asmx?WSDL"
    );

      //RunBenefitsCalculatorOnXML(string xmlInput, int returnFormat));
      $soapresult = $client->RunBenefitsCalculatorOnXML($params);

The comments show the choice of return data. I use the array return because I am having trouble even viewing the xml to even begin to parse it. I can't have THEM  change the choices of return format. But you can certainly try. Note, I do NOT want it as a PDF - I need to parse the return data so I can store it.

Attached is a sample xml that I submit as the xml string, which goes through fine.  The data is a sample, so no confidentiality is breached, but it IS the exact data I send to test.

Thanks for taking a look at this.

Prasad
20100916-eligibility-submit-ID78.xml
0
 

Author Comment

by:iameye
ID: 33698048
Okay, I got most of it:

$count = 9;
$tempr = $soapresult->RunBenefitsCalculatorOnXMLResult->enc_value->BenefitsCalculatorResult;
for ($i=0; $i < $count; $i++) {
      echo($tempr[$i]->Name);
      echo "<br>";
}

I just assigned 9 to the $count variable arbitrarily since doing count($tempr->Name) doesn't work. So now I just need to figure out how to get a count. Or maybe use a while statement or something. It's late - will continue tomorrow...

Prasad
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 33700787
Here is how I would do it.  HTH, ~Ray
<?php // RAY_temp_iameye.php
error_reporting(E_ALL);
echo "<pre>" . PHP_EOL;



// TEST DATA DOWNLOADED FROM EE
$xml_signal = <<<XML
<?xml version="1.0" encoding="iso-8859-1"?>
<agencysystem agencysystemid="12"><household citizenshipid="1" ctresident="1" servicezip="10573" heatingpartofrent="0" outputlanguage="English"><persons><person firstname="Devaprasad" age="40" gender="f" relationshipid="39" preparefoodtogether="1" ethnicityid="2" raceid="7"><assetsummary><assetdetail assetamount="0" assetcategoryid="1"/><assetdetail assetamount="0" assetcategoryid="2"/><assetdetail assetamount="0" assetcategoryid="3"/></assetsummary><incomesummary><incomedetail incomeamount="541.2500" incomecategoryid="1"/><incomedetail incomeamount="232.0000" incomecategoryid="2"/></incomesummary><characteristics><characteristic characteristicid="4" qualifier="0"/><characteristic characteristicid="6" qualifier="0"/><characteristic characteristicid="7" qualifier="0"/><characteristic characteristicid="8" qualifier="0"/><characteristic characteristicid="9" qualifier="1"/><characteristic characteristicid="10" qualifier="0"/><characteristic characteristicid="12" qualifier="0"/><characteristic characteristicid="16" qualifier="0"/><characteristic characteristicid="17" qualifier="0"/><characteristic characteristicid="18" qualifier="0"/><characteristic characteristicid="19" qualifier="0"/><characteristic characteristicid="20" qualifier="1"/></characteristics></person><person firstname="Frank" age="30" gender="m" relationshipid="15" preparefoodtogether="0" ethnicityid="2" raceid="2"><assetsummary><assetdetail assetamount="0" assetcategoryid="1"/><assetdetail assetamount="0" assetcategoryid="2"/><assetdetail assetamount="0" assetcategoryid="3"/></assetsummary><incomesummary><incomedetail incomeamount="0" incomecategoryid="1"/><incomedetail incomeamount="0" incomecategoryid="2"/></incomesummary><characteristics><characteristic characteristicid="3" qualifier="1"/><characteristic characteristicid="4" qualifier="0"/><characteristic characteristicid="6" qualifier="0"/><characteristic characteristicid="7" qualifier="0"/><characteristic characteristicid="8" qualifier="0"/><characteristic characteristicid="9" qualifier="1"/><characteristic characteristicid="10" qualifier="0"/><characteristic characteristicid="12" qualifier="0"/><characteristic characteristicid="16" qualifier="0"/><characteristic characteristicid="17" qualifier="0"/><characteristic characteristicid="18" qualifier="0"/><characteristic characteristicid="19" qualifier="0"/></characteristics></person><person firstname="Kimberly" age="40" gender="f" relationshipid="41" preparefoodtogether="1" ethnicityid="2" raceid="1"><assetsummary><assetdetail assetamount="0" assetcategoryid="1"/><assetdetail assetamount="0" assetcategoryid="2"/><assetdetail assetamount="0" assetcategoryid="3"/></assetsummary><incomesummary><incomedetail incomeamount="0" incomecategoryid="1"/><incomedetail incomeamount="120.0000" incomecategoryid="2"/></incomesummary><characteristics><characteristic characteristicid="2" qualifier="1"/><characteristic characteristicid="4" qualifier="1"/><characteristic characteristicid="6" qualifier="0"/><characteristic characteristicid="8" qualifier="0"/><characteristic characteristicid="9" qualifier="1"/><characteristic characteristicid="10" qualifier="0"/><characteristic characteristicid="12" qualifier="0"/><characteristic characteristicid="16" qualifier="0"/><characteristic characteristicid="17" qualifier="0"/><characteristic characteristicid="18" qualifier="0"/><characteristic characteristicid="19" qualifier="0"/></characteristics></person><person firstname="Briannas" age="16" gender="f" relationshipid="34" preparefoodtogether="0" ethnicityid="2" raceid="7"><assetsummary><assetdetail assetamount="0" assetcategoryid="1"/><assetdetail assetamount="0" assetcategoryid="2"/><assetdetail assetamount="0" assetcategoryid="3"/></assetsummary><incomesummary><incomedetail incomeamount="87.6825" incomecategoryid="1"/><incomedetail incomeamount="217.5825" incomecategoryid="2"/></incomesummary><characteristics><characteristic characteristicid="2" qualifier="1"/><characteristic characteristicid="4" qualifier="1"/><characteristic characteristicid="6" qualifier="1"/><characteristic characteristicid="7" qualifier="0"/><characteristic characteristicid="9" qualifier="1"/><characteristic characteristicid="10" qualifier="0"/><characteristic characteristicid="16" qualifier="0"/><characteristic characteristicid="17" qualifier="0"/><characteristic characteristicid="18" qualifier="0"/><characteristic characteristicid="19" qualifier="0"/></characteristics></person></persons><expensesummary><expensedetail expenseamount="3948" expensecategoryid="3"/><expensedetail expenseamount="3192" expensecategoryid="5"/><expensedetail expenseamount="500" expensecategoryid="1"/></expensesummary></household></agencysystem>
XML;

// TEST CODE COPIED FROM EE - MODIFIED TO USE AND RETURN XML
// SET THE returnFormat: 1->array, 2->html, 3->xml, 4->pdf
$params
= array
( 'xmlInput' => $xml_signal
, 'returnFormat' => 3
)
;

$client
= new SoapClient
( "http://labs.gosmcpartners.com/CAFCA_BenefitsCalculator_WebService/BenefitsCalculator_WebService.asmx?WSDL"
)
;

//RunBenefitsCalculatorOnXML(string xmlInput, int returnFormat));
$soapresult = $client->RunBenefitsCalculatorOnXML($params);

// ACTIVATE THIS TO SEE THE OUTPUT -- RETURNS SOMETHING LIKE THIS:
// object(stdClass)#2 (1) {
//   ["RunBenefitsCalculatorOnXMLResult"]=>
//   string(13984) -- FOLLOWED BY THE XML STRING
// var_dump($soapresult);

// ACQUIRE THE XML STRING
$xml_return = $soapresult->RunBenefitsCalculatorOnXMLResult;

// ACTIVATE THIS TO SEE THE XML STRING
// echo htmlentities($xml_return);

// MAKE AN OBJECT SO WE CAN USE ITERATORS
// NOTE - REMOVE THE @-SIGN TO SEE WARNING MESSAGES ABOUT XMLNS
$obj = @SimpleXML_Load_String($xml_return);

// ACTIVATE THIS TO SEE THE OBJECT
// var_dump($obj);



// FIND OUT INFORMATION ABOUT THE OBJECT
$num = count($obj->program);
echo PHP_EOL . "THERE ARE $num ELEMENTS IN THE RESPONSE" . PHP_EOL;



// ITERATE OVER THE RESULTS
$eligible   = array();
$ineligible = array();
foreach ($obj->program as $program)
{
    // GET AN ELEMENT OF THE PROGRAM
    $l = (string)$program->informationlink;

    // GET ATTRIBUTES OF THE PROGRAM
    $n = (string)$program["name"];
    $c = (string)$program["category"];
    $e = (string)$program["iseligible"];
    $d = (string)$program["displayifineligible"];

    // CREATE A LINK
    $u = $n;
    if (!empty($l))
    {
        $u = '<a target="_blank" href="' . $l . '">' . $n . '</a>';
    }

    // CREATE DISPLAY INFORMATION FOR THIS PROGRAM
    $str = PHP_EOL . $u . ' ';
    if ( strtoupper($d) == 'TRUE')
    {
        $str .= $c;
    }
    else
    {
        $str .= 'REDACTED';
    }

    // PUT THE DISPLAY INFORMATION IN THE CORRECT ARRAY
    if ( strtoupper($e) == 'TRUE')
    {
        $eligible[] = $str;
    }
    else
    {
        $ineligible[] = $str;
    }
}

// SHOW WHAT WE FOUND
echo PHP_EOL . 'ELIGIBLE';
foreach ($eligible as $str) { echo $str; }

echo PHP_EOL;

echo PHP_EOL . 'INELIGIBLE';
foreach ($ineligible as $str) { echo $str; }

Open in new window

0
 

Author Comment

by:iameye
ID: 33705083
Wow - very nice! Will try this out first thing tomorrow morning!
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 33707494
Try it without the @-sign on line 45.  You might want to tell the XML publisher about the warning messages!

Best regards, ~Ray
0
 

Author Comment

by:iameye
ID: 33709334
Thanks Ray!
0
 

Author Closing Comment

by:iameye
ID: 33709337
Very thorough - exactly what was requested.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 33711003
Glad to help!  Best of luck with it, ~Ray
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question