Jump to content

Recommended Posts

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&#32;for&#32;the&#32;ping.</value>
                       </member>
                       <member>
                                <name>legal</name>
                                <value>You&#32;agree&#32;that&#32;use&#32;of&#32;the&#32;Weblogs.com&#32;ping&#32;service&#32;is&#32;governed&#32;by&#32;the&#32;Terms&#32;of&#32;Use&#32;found&#32;at&#32;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.

Link to comment
https://forums.phpfreaks.com/topic/264647-xml-parse-help/
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/264647-xml-parse-help/#findComment-1356341
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.