title: XML reponses in php
body: <p>I am working with a SOAP API that no matter how I try I am unable to get it working with PHP ( I was told by others working with this API that they were not able to get it working with any other language other than Perl which is what it is written in). I had to &quot;hack&quot; a bit so PHP could handle it correctly.</p>
  <p>The code I am using is:</p>
  <pre><code>    &lt;?PHP
  $uid = 'username';
  $pass = 'password';
  $customer_xml = my_api($uid, $pass, 'get_numbers', 'number', 'contains', '1', '0', '0');
  
  function my_api($user, $pass, $func, $request_args){
  
      $soap = new SoapClient(&quot;admin/enswitch_3.9.wsdl&quot;, array('trace' =&gt; true, 'exceptions' =&gt; false));
      $result1 = $soap-&gt;$func($user,$pass,$request_args);
  
      if(isset($soap-&gt;__soap_fault)){
              echo 'There was an error connecting via SOAP. Below is the error:';
              print_r($soap-&gt;__soap_fault-&gt;faultstring);
              }
      elseif(isset($soap-&gt;__last_response)){
  
              echo &quot;Unedited soap response is:\n&quot;;
              echo $soap-&gt;__last_response.&quot;\n&quot;;
              echo &quot;\n\n&quot;;
  
              $xml_string = preg_replace(&quot;/(&lt;\/?)(\w+):([^&gt;]*&gt;)/&quot;, &quot;$1$2$3&quot;, $soap-&gt;__last_response);
              //We need to replace the text s-gensym and then the number with some random text
              //like xoxo so we can then get the value of $xml-&gt;response-&gt;xoxo which should be an array
              $xml_string = preg_replace('/s-gensym[0-9]{0,65000}/', 'xoxo', $xml_string);
  
              $xml = new SimpleXMLElement($xml_string);
  
  
              $xml = $xml-&gt;soapBody;
              $func_response = $func.'Response';
  
              echo '$xml-&gt;$func_response is'.&quot;\n&quot;;
              print_r($xml-&gt;$func_response);
              echo &quot;\n\n&quot;;
  
              echo '$xml-&gt;$func_response-&gt;xoxo is'.&quot;\n&quot;;
              print_r( $xml-&gt;$func_response-&gt;xoxo);
              die();
  
              return $xml-&gt;$func_response-&gt;xoxo;
              }
      }
  ?&gt;
  </code></pre>
  <p>The response that I get from my script code is (phone numbers have been changed to protect them):</p>
  <pre><code>Unedited soap response is:
  
      &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;&lt;soap:Envelope xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:soapenc=&quot;http://schemas.xmlsoap.org/soap/encoding/&quot; xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot; soap:encodingStyle=&quot;http://schemas.xmlsoap.org/soap/encoding/&quot; xmlns:soap=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;&gt;&lt;soap:Body&gt;&lt;get_numbersResponse xmlns=&quot;http://www.example.com/Integrics/Enswitch/API&quot;&gt;&lt;s-gensym3&gt;&lt;maximum_seconds xsi:type=&quot;xsd:string&quot;&gt;-1&lt;/maximum_seconds&gt;&lt;direct xsi:type=&quot;xsd:string&quot;&gt;1&lt;/direct&gt;&lt;fax_dtype xsi:type=&quot;xsd:string&quot;&gt;hangup&lt;/fax_dtype&gt;&lt;stype xsi:type=&quot;xsd:string&quot;&gt;number&lt;/stype&gt;&lt;emergency_register xsi:type=&quot;xsd:string&quot;&gt;0&lt;/emergency_register&gt;&lt;directory xsi:type=&quot;xsd:string&quot;&gt;1&lt;/directory&gt;&lt;display xsi:type=&quot;xsd:string&quot;&gt;12125550432&lt;/display&gt;&lt;sname_action xsi:type=&quot;xsd:string&quot;&gt;replace&lt;/sname_action&gt;&lt;shared xsi:type=&quot;xsd:string&quot;&gt;0&lt;/shared&gt;&lt;callerid xsi:type=&quot;xsd:string&quot; /&gt;&lt;dtype xsi:type=&quot;xsd:string&quot;&gt;queue&lt;/dtype&gt;&lt;sname xsi:type=&quot;xsd:string&quot; /&gt;&lt;dnumber xsi:type=&quot;xsd:string&quot;&gt;696&lt;/dnumber&gt;&lt;fax_detect xsi:type=&quot;xsd:string&quot;&gt;0&lt;/fax_detect&gt;&lt;music xsi:type=&quot;xsd:string&quot;&gt;0&lt;/music&gt;&lt;update_routing xsi:type=&quot;xsd:string&quot;&gt;1&lt;/update_routing&gt;&lt;routing xsi:type=&quot;xsd:string&quot; /&gt;&lt;owner xsi:type=&quot;xsd:string&quot;&gt;0&lt;/owner&gt;&lt;ported xsi:type=&quot;xsd:string&quot;&gt;0&lt;/ported&gt;&lt;language xsi:type=&quot;xsd:string&quot;&gt;en&lt;/language&gt;&lt;fax_dnumber xsi:type=&quot;xsd:string&quot; /&gt;&lt;description xsi:type=&quot;xsd:string&quot; /&gt;&lt;ingroup xsi:type=&quot;xsd:string&quot;&gt;12&lt;/ingroup&gt;&lt;play_message xsi:type=&quot;xsd:string&quot;&gt;0&lt;/play_message&gt;&lt;screen xsi:type=&quot;xsd:string&quot;&gt;0&lt;/screen&gt;&lt;customer xsi:type=&quot;xsd:string&quot;&gt;3935&lt;/customer&gt;&lt;recordgroup xsi:type=&quot;xsd:string&quot;&gt;0&lt;/recordgroup&gt;&lt;class xsi:type=&quot;xsd:string&quot;&gt;212&lt;/class&gt;&lt;shortcut xsi:type=&quot;xsd:string&quot;&gt;1&lt;/shortcut&gt;&lt;alias xsi:type=&quot;xsd:string&quot; /&gt;&lt;snumber xsi:type=&quot;xsd:string&quot;&gt;12125550432&lt;/snumber&gt;&lt;/s-gensym3&gt;&lt;s-gensym5&gt;&lt;maximum_seconds xsi:type=&quot;xsd:string&quot;&gt;-1&lt;/maximum_seconds&gt;&lt;direct xsi:type=&quot;xsd:string&quot;&gt;1&lt;/direct&gt;&lt;fax_dtype xsi:type=&quot;xsd:string&quot;&gt;hangup&lt;/fax_dtype&gt;&lt;stype xsi:type=&quot;xsd:string&quot;&gt;number&lt;/stype&gt;&lt;emergency_register xsi:type=&quot;xsd:string&quot;&gt;0&lt;/emergency_register&gt;&lt;directory xsi:type=&quot;xsd:string&quot;&gt;1&lt;/directory&gt;&lt;display xsi:type=&quot;xsd:string&quot;&gt;12125550458 TEST FOR COLLECT CALL DISABLING&lt;/display&gt;&lt;sname_action xsi:type=&quot;xsd:string&quot;&gt;replace&lt;/sname_action&gt;&lt;shared xsi:type=&quot;xsd:string&quot;&gt;0&lt;/shared&gt;&lt;callerid xsi:type=&quot;xsd:string&quot; /&gt;&lt;dtype xsi:type=&quot;xsd:string&quot;&gt;forward&lt;/dtype&gt;&lt;sname xsi:type=&quot;xsd:string&quot; /&gt;&lt;dnumber xsi:type=&quot;xsd:string&quot;&gt;18005551212&lt;/dnumber&gt;&lt;fax_detect xsi:type=&quot;xsd:string&quot;&gt;0&lt;/fax_detect&gt;&lt;music xsi:type=&quot;xsd:string&quot;&gt;0&lt;/music&gt;&lt;update_routing xsi:type=&quot;xsd:string&quot;&gt;0&lt;/update_routing&gt;&lt;routing xsi:type=&quot;xsd:string&quot; /&gt;&lt;owner xsi:type=&quot;xsd:string&quot;&gt;0&lt;/owner&gt;&lt;ported xsi:type=&quot;xsd:string&quot;&gt;0&lt;/ported&gt;&lt;language xsi:type=&quot;xsd:string&quot;&gt;en&lt;/language&gt;&lt;fax_dnumber xsi:type=&quot;xsd:string&quot; /&gt;&lt;description xsi:type=&quot;xsd:string&quot;&gt;TEST FOR COLLECT CALL DISABLING&lt;/description&gt;&lt;ingroup xsi:type=&quot;xsd:string&quot;&gt;12&lt;/ingroup&gt;&lt;play_message xsi:type=&quot;xsd:string&quot;&gt;0&lt;/play_message&gt;&lt;screen xsi:type=&quot;xsd:string&quot;&gt;0&lt;/screen&gt;&lt;customer xsi:type=&quot;xsd:string&quot;&gt;3935&lt;/customer&gt;&lt;recordgroup xsi:type=&quot;xsd:string&quot;&gt;0&lt;/recordgroup&gt;&lt;class xsi:type=&quot;xsd:string&quot;&gt;212&lt;/class&gt;&lt;shortcut xsi:type=&quot;xsd:string&quot;&gt;1&lt;/shortcut&gt;&lt;alias xsi:type=&quot;xsd:string&quot; /&gt;&lt;snumber xsi:type=&quot;xsd:string&quot;&gt;12125550458&lt;/snumber&gt;&lt;/s-gensym5&gt;&lt;/get_numbersResponse&gt;&lt;/soap:Body&gt;&lt;/soap:Envelope&gt;
  
  $xml-&gt;$func_response is
  SimpleXMLElement Object
  (
      [xoxo] =&gt; Array
      (
          [0] =&gt; SimpleXMLElement Object
              (
                  [maximum_seconds] =&gt; -1
                  [direct] =&gt; 1
                  [fax_dtype] =&gt; hangup
                  [stype] =&gt; number
                  [emergency_register] =&gt; 0
                  [directory] =&gt; 1
                  [display] =&gt; 12125550432
                  [sname_action] =&gt; replace
                  [shared] =&gt; 0
                  [callerid] =&gt; SimpleXMLElement Object
                      (
                      )
  
                  [dtype] =&gt; queue
                  [sname] =&gt; SimpleXMLElement Object
                      (
                      )
  
                  [dnumber] =&gt; 696
                  [fax_detect] =&gt; 0
                  [music] =&gt; 0
                  [update_routing] =&gt; 1
                  [routing] =&gt; SimpleXMLElement Object
                      (
                      )
  
                  [owner] =&gt; 0
                  [ported] =&gt; 0
                  [language] =&gt; en
                  [fax_dnumber] =&gt; SimpleXMLElement Object
                      (
                      )
  
                  [description] =&gt; SimpleXMLElement Object
                      (
                      )
  
                  [ingroup] =&gt; 12
                  [play_message] =&gt; 0
                  [screen] =&gt; 0
                  [customer] =&gt; 3935
                  [recordgroup] =&gt; 0
                  [class] =&gt; 212
                  [shortcut] =&gt; 1
                  [alias] =&gt; SimpleXMLElement Object
                      (
                      )
  
                  [snumber] =&gt; 12125550432
              )
  
          [1] =&gt; SimpleXMLElement Object
              (
                  [maximum_seconds] =&gt; -1
                  [direct] =&gt; 1
                  [fax_dtype] =&gt; hangup
                  [stype] =&gt; number
                  [emergency_register] =&gt; 0
                  [directory] =&gt; 1
                  [display] =&gt; 12125550458 TEST FOR COLLECT CALL DISABLING
                  [sname_action] =&gt; replace
                  [shared] =&gt; 0
                  [callerid] =&gt; SimpleXMLElement Object
                      (
                      )
  
                  [dtype] =&gt; forward
                  [sname] =&gt; SimpleXMLElement Object
                      (
                      )
  
                  [dnumber] =&gt; 18005551212
                  [fax_detect] =&gt; 0
                  [music] =&gt; 0
                  [update_routing] =&gt; 0
                  [routing] =&gt; SimpleXMLElement Object
                      (
                      )
  
                  [owner] =&gt; 0
                  [ported] =&gt; 0
                  [language] =&gt; en
                  [fax_dnumber] =&gt; SimpleXMLElement Object
                      (
                      )
                  [description] =&gt; TEST FOR COLLECT CALL DISABLING
                  [ingroup] =&gt; 12
                  [play_message] =&gt; 0
                  [screen] =&gt; 0
                  [customer] =&gt; 3935
                  [recordgroup] =&gt; 0
                  [class] =&gt; 212
                  [shortcut] =&gt; 1
                  [alias] =&gt; SimpleXMLElement Object
                      (
                      )
                  [snumber] =&gt; 12125550458
              )
      )
  )
  $xml-&gt;$func_response-&gt;xoxo is
  SimpleXMLElement Object
  (
  [maximum_seconds] =&gt; -1
  [direct] =&gt; 1
  [fax_dtype] =&gt; hangup
  [stype] =&gt; number
  [emergency_register] =&gt; 0
  [directory] =&gt; 1
  [display] =&gt; 12125550432
  [sname_action] =&gt; replace
  [shared] =&gt; 0
  [callerid] =&gt; SimpleXMLElement Object
      (
      )
  
  [dtype] =&gt; queue
  [sname] =&gt; SimpleXMLElement Object
      (
      )
  
  [dnumber] =&gt; 696
  [fax_detect] =&gt; 0
  [music] =&gt; 0
  [update_routing] =&gt; 1
  [routing] =&gt; SimpleXMLElement Object
      (
      )
  
  [owner] =&gt; 0
  [ported] =&gt; 0
  [language] =&gt; en
  [fax_dnumber] =&gt; SimpleXMLElement Object
      (
      )
  
  [description] =&gt; SimpleXMLElement Object
      (
      )
  
  [ingroup] =&gt; 12
  [play_message] =&gt; 0
  [screen] =&gt; 0
  [customer] =&gt; 3935
  [recordgroup] =&gt; 0
  [class] =&gt; 212
  [shortcut] =&gt; 1
  [alias] =&gt; SimpleXMLElement Object
      (
      )
  
  [snumber] =&gt; 12125550432
  )
  </code></pre>
  <p>My question is how come $xml-&gt;$func_response will show multiple responses under &quot;element&quot; xoxo but if I try to print_r on $xml-&gt;func_response-&gt;xoxo I only get one response. Interesting enough if I do:</p>
  <pre><code>    foreach($xml-&gt;func_response as $y){
          foreach($y as $z){
            print_r($z)
            }
           }
          }
  </code></pre>
  <p>Then I do get the value for each one but it seems backward to do it that way.</p>
  <hr />
  <h3>Reformatted XML data</h3>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;soap:Envelope xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
      xmlns:soapenc=&quot;http://schemas.xmlsoap.org/soap/encoding/&quot;
      xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;
      soap:encodingStyle=&quot;http://schemas.xmlsoap.org/soap/encoding/&quot;
      xmlns:soap=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;&gt;
      &lt;soap:Body&gt;
          &lt;get_numbersResponse xmlns=&quot;http://www.example.com/Integrics/Enswitch/API&quot;&gt;
              &lt;s-gensym3&gt;
                  &lt;maximum_seconds xsi:type=&quot;xsd:string&quot;&gt;-1&lt;/maximum_seconds&gt;
                  &lt;direct xsi:type=&quot;xsd:string&quot;&gt;1&lt;/direct&gt;
                  &lt;fax_dtype xsi:type=&quot;xsd:string&quot;&gt;hangup&lt;/fax_dtype&gt;
                  &lt;stype xsi:type=&quot;xsd:string&quot;&gt;number&lt;/stype&gt;
                  &lt;emergency_register xsi:type=&quot;xsd:string&quot;&gt;0&lt;/emergency_register&gt;
                  &lt;directory xsi:type=&quot;xsd:string&quot;&gt;1&lt;/directory&gt;
                  &lt;display xsi:type=&quot;xsd:string&quot;&gt;12125550432&lt;/display&gt;
                  &lt;sname_action xsi:type=&quot;xsd:string&quot;&gt;replace&lt;/sname_action&gt;
                  &lt;shared xsi:type=&quot;xsd:string&quot;&gt;0&lt;/shared&gt;
                  &lt;callerid xsi:type=&quot;xsd:string&quot;/&gt;
                  &lt;dtype xsi:type=&quot;xsd:string&quot;&gt;queue&lt;/dtype&gt;
                  &lt;sname xsi:type=&quot;xsd:string&quot;/&gt;
                  &lt;dnumber xsi:type=&quot;xsd:string&quot;&gt;696&lt;/dnumber&gt;
                  &lt;fax_detect xsi:type=&quot;xsd:string&quot;&gt;0&lt;/fax_detect&gt;
                  &lt;music xsi:type=&quot;xsd:string&quot;&gt;0&lt;/music&gt;
                  &lt;update_routing xsi:type=&quot;xsd:string&quot;&gt;1&lt;/update_routing&gt;
                  &lt;routing xsi:type=&quot;xsd:string&quot;/&gt;
                  &lt;owner xsi:type=&quot;xsd:string&quot;&gt;0&lt;/owner&gt;
                  &lt;ported xsi:type=&quot;xsd:string&quot;&gt;0&lt;/ported&gt;
                  &lt;language xsi:type=&quot;xsd:string&quot;&gt;en&lt;/language&gt;
                  &lt;fax_dnumber xsi:type=&quot;xsd:string&quot;/&gt;
                  &lt;description xsi:type=&quot;xsd:string&quot;/&gt;
                  &lt;ingroup xsi:type=&quot;xsd:string&quot;&gt;12&lt;/ingroup&gt;
                  &lt;play_message xsi:type=&quot;xsd:string&quot;&gt;0&lt;/play_message&gt;
                  &lt;screen xsi:type=&quot;xsd:string&quot;&gt;0&lt;/screen&gt;
                  &lt;customer xsi:type=&quot;xsd:string&quot;&gt;3935&lt;/customer&gt;
                  &lt;recordgroup xsi:type=&quot;xsd:string&quot;&gt;0&lt;/recordgroup&gt;
                  &lt;class xsi:type=&quot;xsd:string&quot;&gt;212&lt;/class&gt;
                  &lt;shortcut xsi:type=&quot;xsd:string&quot;&gt;1&lt;/shortcut&gt;
                  &lt;alias xsi:type=&quot;xsd:string&quot;/&gt;
                  &lt;snumber xsi:type=&quot;xsd:string&quot;&gt;12125550432&lt;/snumber&gt;
              &lt;/s-gensym3&gt;
              &lt;s-gensym5&gt;
                  &lt;maximum_seconds xsi:type=&quot;xsd:string&quot;&gt;-1&lt;/maximum_seconds&gt;
                  &lt;direct xsi:type=&quot;xsd:string&quot;&gt;1&lt;/direct&gt;
                  &lt;fax_dtype xsi:type=&quot;xsd:string&quot;&gt;hangup&lt;/fax_dtype&gt;
                  &lt;stype xsi:type=&quot;xsd:string&quot;&gt;number&lt;/stype&gt;
                  &lt;emergency_register xsi:type=&quot;xsd:string&quot;&gt;0&lt;/emergency_register&gt;
                  &lt;directory xsi:type=&quot;xsd:string&quot;&gt;1&lt;/directory&gt;
                  &lt;display xsi:type=&quot;xsd:string&quot;&gt;12125550458 TEST FOR COLLECT CALL DISABLING&lt;/display&gt;
                  &lt;sname_action xsi:type=&quot;xsd:string&quot;&gt;replace&lt;/sname_action&gt;
                  &lt;shared xsi:type=&quot;xsd:string&quot;&gt;0&lt;/shared&gt;
                  &lt;callerid xsi:type=&quot;xsd:string&quot;/&gt;
                  &lt;dtype xsi:type=&quot;xsd:string&quot;&gt;forward&lt;/dtype&gt;
                  &lt;sname xsi:type=&quot;xsd:string&quot;/&gt;
                  &lt;dnumber xsi:type=&quot;xsd:string&quot;&gt;18005551212&lt;/dnumber&gt;
                  &lt;fax_detect xsi:type=&quot;xsd:string&quot;&gt;0&lt;/fax_detect&gt;
                  &lt;music xsi:type=&quot;xsd:string&quot;&gt;0&lt;/music&gt;
                  &lt;update_routing xsi:type=&quot;xsd:string&quot;&gt;0&lt;/update_routing&gt;
                  &lt;routing xsi:type=&quot;xsd:string&quot;/&gt;
                  &lt;owner xsi:type=&quot;xsd:string&quot;&gt;0&lt;/owner&gt;
                  &lt;ported xsi:type=&quot;xsd:string&quot;&gt;0&lt;/ported&gt;
                  &lt;language xsi:type=&quot;xsd:string&quot;&gt;en&lt;/language&gt;
                  &lt;fax_dnumber xsi:type=&quot;xsd:string&quot;/&gt;
                  &lt;description xsi:type=&quot;xsd:string&quot;&gt;TEST FOR COLLECT CALL DISABLING&lt;/description&gt;
                  &lt;ingroup xsi:type=&quot;xsd:string&quot;&gt;12&lt;/ingroup&gt;
                  &lt;play_message xsi:type=&quot;xsd:string&quot;&gt;0&lt;/play_message&gt;
                  &lt;screen xsi:type=&quot;xsd:string&quot;&gt;0&lt;/screen&gt;
                  &lt;customer xsi:type=&quot;xsd:string&quot;&gt;3935&lt;/customer&gt;
                  &lt;recordgroup xsi:type=&quot;xsd:string&quot;&gt;0&lt;/recordgroup&gt;
                  &lt;class xsi:type=&quot;xsd:string&quot;&gt;212&lt;/class&gt;
                  &lt;shortcut xsi:type=&quot;xsd:string&quot;&gt;1&lt;/shortcut&gt;
                  &lt;alias xsi:type=&quot;xsd:string&quot;/&gt;
                  &lt;snumber xsi:type=&quot;xsd:string&quot;&gt;12125550458&lt;/snumber&gt;
              &lt;/s-gensym5&gt;
          &lt;/get_numbersResponse&gt;
      &lt;/soap:Body&gt;
  &lt;/soap:Envelope&gt;
  </code></pre>