XML 回复 php

XML reponses in php

我正在使用 SOAP API,无论我如何尝试,我都无法让它与 PHP 一起工作(有人告诉我使用这个 API他们无法让它与除 Perl 以外的任何其他语言一起使用,这是它所写的)。我不得不“破解”一下,以便 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+):([^>]*>)/", "", $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;
            }
    }
?>

我从脚本代码中得到的响应是(phone 数字已更改以保护它们):

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->$func_response 为什么会在“元素”xoxo 下显示多个回复,但是如果我尝试 print_r 在 $xml ->func_response->xoxo 我只收到一个回复​​。如果我这样做就足够有趣了:

    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>

没有什么理由,就是因为SOAP接口是用Perl实现的,所以你需要用Perl来写你的SOAP客户端

您没有解释为什么要删除名称空间,或将所有 s-gensym* 元素更改为 xoxo,但这没有必要

有一个陷阱:如果你想在 XPath 表达式中使用非空命名空间,那么你必须 name 前缀和 use 它在你的代码中。元素

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

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

你没有说你想要什么数据,但我已经从s-gensym3s-gensym5中提取了display字段。我的代码从文件 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