Solved

PHP Parse SOAP Data

Posted on 2010-09-14
13
509 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
 

Author Comment

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

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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 108

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 108

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 108

Expert Comment

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now