Forum Moderators: coopster

Message Too Old, No Replies

Parsing XML Response Object with PHP

parsing xml objects with PHP

         

lenglish

8:00 pm on Nov 24, 2010 (gmt 0)

10+ Year Member



Parsing SoapClient Response with PHP

Good afternoon and Happy Thanksgiving to all!

I am a new member of this board and this is my first post. :-)

I am not new to PHP development, but fairly new to parsing SOAP objects, so here's my story:

I have successfully built a SoapClient, which returns an object as expected:

$client = new SoapClient ("https://pathtowsdl.asmx?wsdl",array("trace"=>1,'encoding'=>'utf-8'));


$response = $client -> SearchKBArticlesUnderFolder($params);



Below is a sample of the var_dump of the returned object with some of the actual data replaced with dummy values:

object(stdClass)#2 (1) {
["SearchKBArticlesUnderFolderResult"]=>
object(stdClass)#3 (4) {
["Articles"]=>
object(stdClass)#4 (1) {
["KBArticlesResultPart"]=>
array(3) {
[0]=>
object(stdClass)#5 (5) {
["BodySnippet"]=>
string(509) "long string value"
["KBArticleID"]=>
int(381)
["Relevance"]=>
float(100)
["Subject"]=>
string(95) "another string value..."
["Url"]=>
string(93) "https://urltoarticle.aspx"

My question / problem is this:

I cannot successfully parse the object so I can display individual elements in a results page.

I have tried various ways to do this, but I'm really not having a lot of luck.

Any pointers to other articles, list of steps, etc. would be greatly appreciated.

astupidname

7:56 am on Nov 25, 2010 (gmt 0)

10+ Year Member



Hi lenglish, welcome to webmasterworld, and happy thanksgiving to you too!
$response, when you format it out a bit, appears to have this basic structure (where '{' denotes start of an object and '}' end of an object):

{
SearchKBArticlesUnderFolderResult => {
Articles => {
KBArticlesResultPart => array(
{
BodySnippet => "long string value",
KBArticleID => 381,
Relevance => 100,
Subject => "another string value...",
Url => "https://urltoarticle.aspx"
}
)
}
}
}


Which means I would think you should be able to access the KBArticlesResultPart array values like so:

$arrItemZero = $response->SearchKBArticlesUnderFolderResult->Articles->KBArticlesResultPart[0];
echo $arrItemZero->KBArticleID.'<br>'.$arrItemZero->BodySnippet.'<br>'.$arrItemZero->Subject;


If anybody's wondering, how to post formatted code on webmasterworld [webmasterworld.com]
Do not copy formatted code on webmasterworld from IE, use other browser such as Firefox.