0

How to store SOAP returned value in PHP?

My PHP code using SOAP.

    <?php
        $client = new SoapClient("http://rscnagahrd/OracleWS/Oracle.asmx?WSDL");
        $result = $client->getFoods();
        echo $result;
    ?>

When I try to run the application I get the error:

Catchable fatal error: Object of class stdClass could not be converted to string in C:\apache\htdocs\food.php on line 4

I am doing this so that I could later parse the xml code:

$dom = new DomDocument;
$dom->preserveWhiteSpace = FALSE;
$dom->loadXML($result);
$params = $dom->getElementsByTagName('FOOD_ID');
    foreach ($params as $param) {
    echo $param -> nodeValue, PHP_EOL;
}

1 Answer 1

2

The Soapclient object already has parsed the XML to a response structure (var_dump($result) and see). If you want to store it as a string, you'd have make it a string again somehow (serialize() if only PHP code needs to read it, json_encode() if you want to make it more language-agnostic are the usual suspects).

If you need to store the raw xml response, add array('trace' => true); as the 2nd parameter in SoapClient's constructor, and call $client->__getLastRequest();.

But, if you just want the answer used then & there the $result response is already parsed for you.

Sign up to request clarification or add additional context in comments.

10 Comments

Its not the answer I was expecting, but it made sense that I was not asking the right question. Thanks
Ah, what question did you mean to ask?
Inside $result is an xml name food_name, food_description, etc, how can i extract them and place them into a php variable, debugging in visual studio returns something like: <Table diffgr:id="Table1" msdata:rowOrder="0"> <FOOD_ID> 1 </FOOD_ID> <FOOD_NAME> Burger </FOOD_NAME> <FOOD_DESC> 100% Beef Patty </FOOD_DESC> <FOOD_PRICE> 25 </FOOD_PRICE> </Table> <Table diffgr:id="Table2" msdata:rowOrder="1"> <FOOD_ID> 2 </FOOD_ID> <FOOD_NAME> Coke </FOOD_NAME> <FOOD_DESC> Drinks </FOOD_DESC> <FOOD_PRICE> 10 </FOOD_PRICE> </Table>
It depends on whether that is sent as actual xml in the soap response (so you'd acces it something like foreach($result->Table as $food) echo $food->FOOD_ID.':'.$food->FOOD_NAME, or as encoded xml (i.e: it will be as a string in some location in the struct, and we need to build in object out of it). If you put the results of var_dump($result); up somewhere on pastebin.com or something like that, I could tell you more.
var_dum($result). I have also tried foreach($result->Table as $food) echo $food->FOOD_ID.':'.$food->FOOD_NAME but i am getting an error foreach
|

Your Answer

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.