Ricky. Posted June 23, 2012 Share Posted June 23, 2012 Hi I am kind of stuck at parsing xml-rpc response, it generally sends rsponse with following ways. 1. If PING is failed, you receive faultcode or sometimes only code with faultstring, like $errorStr1 I have given below in code block. 2. If ping is successful, you receive "flerror" , with message like I have mentiond as string in code block below as "$flResponse and $flResponse" <?php $errorStr1 = '<?xml version="1.0"?> <methodResponse> <fault> <value> <struct> <member> <name>faultCode</name> <value><int>4</int></value> </member> <member> <name>faultString</name> <value><string>Too many parameters.</string></value> </member> </struct> </value> </fault> </methodResponse>'; $flResponse = '<?xml version="1.0"?> <methodResponse> <params> <param> <value> <struct> <member> <name>flerror</name> <value><boolean>0</boolean></value> </member> <member> <name>message</name> <value>Thanks for the ping.</value> </member> </struct> </value> </param> </params> </methodResponse>'; $flresponse2 = '<?xml version="1.0"?> <methodResponse> <params> <param> <value> <struct> <member> <name>message</name> <value>Thanks for the ping.</value> </member> <member> <name>legal</name> <value>You agree that use of the Weblogs.com ping service is governed by the Terms of Use found at www.weblogs.com. </value> </member> <member> <name>flerror</name> <value><boolean>0</boolean> </value> </member> </struct> </value> </param> </params> </methodResponse>'; php?> Now problem is that no matter what I do, I can't make good use of it. What I need to do is if xml-RPC response contains following : <member> <name>flerror</name> <value><boolean>0</boolean></value> </member> <member> <name>message</name> <value>Thanks for the ping.</value> </member> I need to take out flerror with its value and message with its value. I am fiddling with SimpleXML, XMLreader, DOMdocument but can't find best way to do it. For example if we have : <member> <name>faultCode</name> <value><int>4</int></value> </member> <member> <name>faultString</name> <value><string>Too many parameters.</string></value> </member> in response then it means it was problem. If anyone with experience in this can help then would be great.. To make it clear, the response code varies from service to service, there always some tags missing or added so I can't directly access them simply using SimpleXML object, I need to find if certain tag exist then get its child values. Quote Link to comment https://forums.phpfreaks.com/topic/264647-xml-parse-help/ Share on other sites More sharing options...
requinix Posted June 23, 2012 Share Posted June 23, 2012 And everything mentioned so far is totally possible with SimpleXML. What kind of code do you have? Quote Link to comment https://forums.phpfreaks.com/topic/264647-xml-parse-help/#findComment-1356338 Share on other sites More sharing options...
Ricky. Posted June 23, 2012 Author Share Posted June 23, 2012 I have not settled with any code yet, with simple xml I can directly access any node value like this : (say we have following string) $responseXML = '<?xml version="1.0"?> <methodResponse> <fault> <value> <struct> <member> <name>faultCode</name> <value><int>4</int></value> </member> <member> <name>faultString</name> <value><string>Too many parameters.</string></value> </member> </struct> </value> </fault> </methodResponse>'; $xml = new SimpleXMLElement($responseXML); echo '<pre>'; print_r($xml); echo '</pre>'; $fault = $xml->fault->value->struct->member[0]->name; $faultValue = $xml->fault->value->struct->member[0]->value->int; echo $fault , '<br />' , $faultValue; [/code] Now it will output faultCode 4 Certainly this way I can get value and using conditions , I can get to know what was response but issue is that node structure changes from service to service, sometimes there is no <fault> code at all, sometimes nesting structure is little different. Basically I need to find if response xml has "faultCode" or "Code" , then what is in next node which contains real faultcode, same goes with FaultString, I need to find if it has node with value faultstring and then what is value? Quote Link to comment https://forums.phpfreaks.com/topic/264647-xml-parse-help/#findComment-1356341 Share on other sites More sharing options...
Barand Posted June 23, 2012 Share Posted June 23, 2012 <?php $xml = simplexml_load_string($str); foreach ($xml->xpath('//member') as $m) { if ($m->name == 'flerror') echo $m->name . ' : ' . $m->value->boolean . '<br />'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/264647-xml-parse-help/#findComment-1356346 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.