drs3925 2015-07-27 10:43
浏览 57
已采纳

php中的XML响应

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 "hack" a bit so PHP could handle it correctly.

The code I am using is:

    <?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("admin/enswitch_3.9.wsdl", array('trace' => true, 'exceptions' => false));
    $result1 = $soap->$func($user,$pass,$request_args);

    if(isset($soap->__soap_fault)){
            echo 'There was an error connecting via SOAP. Below is the error:';
            print_r($soap->__soap_fault->faultstring);
            }
    elseif(isset($soap->__last_response)){

            echo "Unedited soap response is:
";
            echo $soap->__last_response."
";
            echo "

";

            $xml_string = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $soap->__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->response->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->soapBody;
            $func_response = $func.'Response';

            echo '$xml->$func_response is'."
";
            print_r($xml->$func_response);
            echo "

";

            echo '$xml->$func_response->xoxo is'."
";
            print_r( $xml->$func_response->xoxo);
            die();

            return $xml->$func_response->xoxo;
            }
    }
?>

The response that I get from my script code is (phone numbers have been changed to protect them):

Unedited soap response is:

    <?xml version="1.0" encoding="UTF-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><get_numbersResponse xmlns="http://www.example.com/Integrics/Enswitch/API"><s-gensym3><maximum_seconds xsi:type="xsd:string">-1</maximum_seconds><direct xsi:type="xsd:string">1</direct><fax_dtype xsi:type="xsd:string">hangup</fax_dtype><stype xsi:type="xsd:string">number</stype><emergency_register xsi:type="xsd:string">0</emergency_register><directory xsi:type="xsd:string">1</directory><display xsi:type="xsd:string">12125550432</display><sname_action xsi:type="xsd:string">replace</sname_action><shared xsi:type="xsd:string">0</shared><callerid xsi:type="xsd:string" /><dtype xsi:type="xsd:string">queue</dtype><sname xsi:type="xsd:string" /><dnumber xsi:type="xsd:string">696</dnumber><fax_detect xsi:type="xsd:string">0</fax_detect><music xsi:type="xsd:string">0</music><update_routing xsi:type="xsd:string">1</update_routing><routing xsi:type="xsd:string" /><owner xsi:type="xsd:string">0</owner><ported xsi:type="xsd:string">0</ported><language xsi:type="xsd:string">en</language><fax_dnumber xsi:type="xsd:string" /><description xsi:type="xsd:string" /><ingroup xsi:type="xsd:string">12</ingroup><play_message xsi:type="xsd:string">0</play_message><screen xsi:type="xsd:string">0</screen><customer xsi:type="xsd:string">3935</customer><recordgroup xsi:type="xsd:string">0</recordgroup><class xsi:type="xsd:string">212</class><shortcut xsi:type="xsd:string">1</shortcut><alias xsi:type="xsd:string" /><snumber xsi:type="xsd:string">12125550432</snumber></s-gensym3><s-gensym5><maximum_seconds xsi:type="xsd:string">-1</maximum_seconds><direct xsi:type="xsd:string">1</direct><fax_dtype xsi:type="xsd:string">hangup</fax_dtype><stype xsi:type="xsd:string">number</stype><emergency_register xsi:type="xsd:string">0</emergency_register><directory xsi:type="xsd:string">1</directory><display xsi:type="xsd:string">12125550458 TEST FOR COLLECT CALL DISABLING</display><sname_action xsi:type="xsd:string">replace</sname_action><shared xsi:type="xsd:string">0</shared><callerid xsi:type="xsd:string" /><dtype xsi:type="xsd:string">forward</dtype><sname xsi:type="xsd:string" /><dnumber xsi:type="xsd:string">18005551212</dnumber><fax_detect xsi:type="xsd:string">0</fax_detect><music xsi:type="xsd:string">0</music><update_routing xsi:type="xsd:string">0</update_routing><routing xsi:type="xsd:string" /><owner xsi:type="xsd:string">0</owner><ported xsi:type="xsd:string">0</ported><language xsi:type="xsd:string">en</language><fax_dnumber xsi:type="xsd:string" /><description xsi:type="xsd:string">TEST FOR COLLECT CALL DISABLING</description><ingroup xsi:type="xsd:string">12</ingroup><play_message xsi:type="xsd:string">0</play_message><screen xsi:type="xsd:string">0</screen><customer xsi:type="xsd:string">3935</customer><recordgroup xsi:type="xsd:string">0</recordgroup><class xsi:type="xsd:string">212</class><shortcut xsi:type="xsd:string">1</shortcut><alias xsi:type="xsd:string" /><snumber xsi:type="xsd:string">12125550458</snumber></s-gensym5></get_numbersResponse></soap:Body></soap:Envelope>

$xml->$func_response is
SimpleXMLElement Object
(
    [xoxo] => Array
    (
        [0] => SimpleXMLElement Object
            (
                [maximum_seconds] => -1
                [direct] => 1
                [fax_dtype] => hangup
                [stype] => number
                [emergency_register] => 0
                [directory] => 1
                [display] => 12125550432
                [sname_action] => replace
                [shared] => 0
                [callerid] => SimpleXMLElement Object
                    (
                    )

                [dtype] => queue
                [sname] => SimpleXMLElement Object
                    (
                    )

                [dnumber] => 696
                [fax_detect] => 0
                [music] => 0
                [update_routing] => 1
                [routing] => SimpleXMLElement Object
                    (
                    )

                [owner] => 0
                [ported] => 0
                [language] => en
                [fax_dnumber] => SimpleXMLElement Object
                    (
                    )

                [description] => SimpleXMLElement Object
                    (
                    )

                [ingroup] => 12
                [play_message] => 0
                [screen] => 0
                [customer] => 3935
                [recordgroup] => 0
                [class] => 212
                [shortcut] => 1
                [alias] => SimpleXMLElement Object
                    (
                    )

                [snumber] => 12125550432
            )

        [1] => SimpleXMLElement Object
            (
                [maximum_seconds] => -1
                [direct] => 1
                [fax_dtype] => hangup
                [stype] => number
                [emergency_register] => 0
                [directory] => 1
                [display] => 12125550458 TEST FOR COLLECT CALL DISABLING
                [sname_action] => replace
                [shared] => 0
                [callerid] => SimpleXMLElement Object
                    (
                    )

                [dtype] => forward
                [sname] => SimpleXMLElement Object
                    (
                    )

                [dnumber] => 18005551212
                [fax_detect] => 0
                [music] => 0
                [update_routing] => 0
                [routing] => SimpleXMLElement Object
                    (
                    )

                [owner] => 0
                [ported] => 0
                [language] => en
                [fax_dnumber] => SimpleXMLElement Object
                    (
                    )
                [description] => TEST FOR COLLECT CALL DISABLING
                [ingroup] => 12
                [play_message] => 0
                [screen] => 0
                [customer] => 3935
                [recordgroup] => 0
                [class] => 212
                [shortcut] => 1
                [alias] => SimpleXMLElement Object
                    (
                    )
                [snumber] => 12125550458
            )
    )
)
$xml->$func_response->xoxo is
SimpleXMLElement Object
(
[maximum_seconds] => -1
[direct] => 1
[fax_dtype] => hangup
[stype] => number
[emergency_register] => 0
[directory] => 1
[display] => 12125550432
[sname_action] => replace
[shared] => 0
[callerid] => SimpleXMLElement Object
    (
    )

[dtype] => queue
[sname] => SimpleXMLElement Object
    (
    )

[dnumber] => 696
[fax_detect] => 0
[music] => 0
[update_routing] => 1
[routing] => SimpleXMLElement Object
    (
    )

[owner] => 0
[ported] => 0
[language] => en
[fax_dnumber] => SimpleXMLElement Object
    (
    )

[description] => SimpleXMLElement Object
    (
    )

[ingroup] => 12
[play_message] => 0
[screen] => 0
[customer] => 3935
[recordgroup] => 0
[class] => 212
[shortcut] => 1
[alias] => SimpleXMLElement Object
    (
    )

[snumber] => 12125550432
)

My question is how come $xml->$func_response will show multiple responses under "element" xoxo but if I try to print_r on $xml->func_response->xoxo I only get one response. Interesting enough if I do:

    foreach($xml->func_response as $y){
        foreach($y as $z){
          print_r($z)
          }
         }
        }

Then I do get the value for each one but it seems backward to do it that way.


Reformatted XML data

<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <get_numbersResponse xmlns="http://www.example.com/Integrics/Enswitch/API">
            <s-gensym3>
                <maximum_seconds xsi:type="xsd:string">-1</maximum_seconds>
                <direct xsi:type="xsd:string">1</direct>
                <fax_dtype xsi:type="xsd:string">hangup</fax_dtype>
                <stype xsi:type="xsd:string">number</stype>
                <emergency_register xsi:type="xsd:string">0</emergency_register>
                <directory xsi:type="xsd:string">1</directory>
                <display xsi:type="xsd:string">12125550432</display>
                <sname_action xsi:type="xsd:string">replace</sname_action>
                <shared xsi:type="xsd:string">0</shared>
                <callerid xsi:type="xsd:string"/>
                <dtype xsi:type="xsd:string">queue</dtype>
                <sname xsi:type="xsd:string"/>
                <dnumber xsi:type="xsd:string">696</dnumber>
                <fax_detect xsi:type="xsd:string">0</fax_detect>
                <music xsi:type="xsd:string">0</music>
                <update_routing xsi:type="xsd:string">1</update_routing>
                <routing xsi:type="xsd:string"/>
                <owner xsi:type="xsd:string">0</owner>
                <ported xsi:type="xsd:string">0</ported>
                <language xsi:type="xsd:string">en</language>
                <fax_dnumber xsi:type="xsd:string"/>
                <description xsi:type="xsd:string"/>
                <ingroup xsi:type="xsd:string">12</ingroup>
                <play_message xsi:type="xsd:string">0</play_message>
                <screen xsi:type="xsd:string">0</screen>
                <customer xsi:type="xsd:string">3935</customer>
                <recordgroup xsi:type="xsd:string">0</recordgroup>
                <class xsi:type="xsd:string">212</class>
                <shortcut xsi:type="xsd:string">1</shortcut>
                <alias xsi:type="xsd:string"/>
                <snumber xsi:type="xsd:string">12125550432</snumber>
            </s-gensym3>
            <s-gensym5>
                <maximum_seconds xsi:type="xsd:string">-1</maximum_seconds>
                <direct xsi:type="xsd:string">1</direct>
                <fax_dtype xsi:type="xsd:string">hangup</fax_dtype>
                <stype xsi:type="xsd:string">number</stype>
                <emergency_register xsi:type="xsd:string">0</emergency_register>
                <directory xsi:type="xsd:string">1</directory>
                <display xsi:type="xsd:string">12125550458 TEST FOR COLLECT CALL DISABLING</display>
                <sname_action xsi:type="xsd:string">replace</sname_action>
                <shared xsi:type="xsd:string">0</shared>
                <callerid xsi:type="xsd:string"/>
                <dtype xsi:type="xsd:string">forward</dtype>
                <sname xsi:type="xsd:string"/>
                <dnumber xsi:type="xsd:string">18005551212</dnumber>
                <fax_detect xsi:type="xsd:string">0</fax_detect>
                <music xsi:type="xsd:string">0</music>
                <update_routing xsi:type="xsd:string">0</update_routing>
                <routing xsi:type="xsd:string"/>
                <owner xsi:type="xsd:string">0</owner>
                <ported xsi:type="xsd:string">0</ported>
                <language xsi:type="xsd:string">en</language>
                <fax_dnumber xsi:type="xsd:string"/>
                <description xsi:type="xsd:string">TEST FOR COLLECT CALL DISABLING</description>
                <ingroup xsi:type="xsd:string">12</ingroup>
                <play_message xsi:type="xsd:string">0</play_message>
                <screen xsi:type="xsd:string">0</screen>
                <customer xsi:type="xsd:string">3935</customer>
                <recordgroup xsi:type="xsd:string">0</recordgroup>
                <class xsi:type="xsd:string">212</class>
                <shortcut xsi:type="xsd:string">1</shortcut>
                <alias xsi:type="xsd:string"/>
                <snumber xsi:type="xsd:string">12125550458</snumber>
            </s-gensym5>
        </get_numbersResponse>
    </soap:Body>
</soap:Envelope>
  • 写回答

1条回答 默认 最新

  • dongpao5658 2015-07-27 12:33
    关注

    There is no reason why, just because SOAP interface was implemented in Perl, you need to use Perl to write your SOAP client

    You don't explain why you're removing the namespaces, or changing all of the s-gensym* elements to xoxo but it isn't necessary

    There is one gotcha: if you want to use a non-blank namespace in an XPath expression then you have to name a prefix and use it in your code. The element

    <get_numbersResponse xmlns="http://www.example.com/Integrics/Enswitch/API"> ... </get_numbersResponse>
    

    has a blank default namespace, so I have bound the prefix def to it in my code below so that it can now be referred to as def:get_numbersResponse. Apart from that you can simply navigate the XML data using XPath expressions. The soap namespace prefix is already defined by the XML, so I can refer to soap:Envelope and soap:Body without defining it in my code

    You don't say what data you want, but I have extracted the display field from both s-gensym3 and s-gensym5. And my code reads the XML from the file soap.xml; I assume you can use this code to work with the data from your SOAP client object?

    <?php
    
      $xml = new SimpleXMLElement('soap.xml', NULL, TRUE);
      $xml->registerXPathNamespace('def', 'http://www.example.com/Integrics/Enswitch/API');
      $numbers_response = $xml->xpath('/soap:Envelope/soap:Body/def:get_numbersResponse/*');
    
      while (list($i, $node) = each($numbers_response)) {
        print("
    ");
        printf("%d: %s
    ", $i, $node->getName());
        printf("    %s
    ", $node->display);
      }
    ?>
    

    output

    0: s-gensym3
        12125550432
    
    1: s-gensym5
        12125550458 TEST FOR COLLECT CALL DISABLING
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化