XML在PHP中响应

时间:2015-07-27 10:43:47

标签: php xml soap simplexml

我正在使用SOAP API,无论我如何尝试我都无法使用PHP(使用此API的其他人告诉我们,他们无法使用除了以外的任何其他语言Perl就是它写的内容)。我不得不" hack"有点因此PHP可以正确处理它。

我使用的代码是:

    <?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:\n";
            echo $soap->__last_response."\n";
            echo "\n\n";

            $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'."\n";
            print_r($xml->$func_response);
            echo "\n\n";

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

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

我从脚本代码中得到的回复是(电话号码已被更改以保护它们):

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
)

我的问题是$ xml-&gt; $ func_response将如何在&#34;元素&#34;下显示多个响应。 xoxo但是如果我尝试在$ xml-&gt; func_response-&gt; xoxo上print_r我只得到一个响应。如果我这样做有趣的话:

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

然后我确实得到了每一个的值,但这样做似乎是落后的。

重新格式化的XML数据

<?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 个答案:

答案 0 :(得分:0)

没有理由,因为SOAP接口是在Perl中实现的,您需要使用Perl来编写SOAP客户端

您无法解释为什么要删除命名空间,或将所有s-gensym*元素更改为xoxo,但这不是必需的

有一个问题:如果要在XPath表达式中使用非空命名空间,则必须在代码中命名一个前缀并使用它。元素

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

有一个空白的默认命名空间,所以我在下面的代码中将前缀def绑定到它,以便它现在可以称为def:get_numbersResponse。除此之外,您可以使用XPath表达式简单地导航XML数据。 soap名称空间前缀已由XML定义,因此我可以参考soap:Envelopesoap:Body而无需在我的代码中定义

您不会说出您想要的数据,但我已从displays-gensym3中提取s-gensym5字段。我的代码从文件soap.xml中读取XML;我假设您可以使用此代码处理来自SOAP客户端对象的数据?

<?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("\n");
    printf("%d: %s\n", $i, $node->getName());
    printf("    %s\n", $node->display);
  }
?>

输出

0: s-gensym3
    12125550432

1: s-gensym5
    12125550458 TEST FOR COLLECT CALL DISABLING