PHP和Simplecml_load_string和Xpath来回显特定的值

时间:2015-08-11 13:16:11

标签: php xml xpath namespaces simplexml

我有以下SOAP响应:

<?xml version="1.0" encoding="utf-8"?>

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">

<soap:Header>
<wsa:Action>RetrieveResponse</wsa:Action>
<wsa:MessageID>urn:uuid:4bd57838-61fd-4edf-8c66-XXXXXXXX</wsa:MessageID>
<wsa:RelatesTo>urn:uuid:eaa150ab-fe7f-4b9b-855f-YYYYYYYY</wsa:RelatesTo>
<wsa:To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:To>
<wsse:Security><wsu:Timestamp wsu:Id="Timestamp-bcd6b0b0-9fc0-45a1-9363-AAAAAA"><wsu:Created>2015-08-11T09:06:40Z</wsu:Created><wsu:Expires>2015-08-11T09:11:40Z</wsu:Expires></wsu:Timestamp></wsse:Security>
</soap:Header>

<soap:Body>
<RetrieveResponseMsg xmlns="http://exacttarget.com/wsdl/partnerAPI">
    <OverallStatus>OK</OverallStatus>
    <RequestID>12af20a5-b76e-4043-ae76-XXXXXXXX</RequestID>
    <Results xsi:type="Subscriber">
        <PartnerKey xsi:nil="true" />
        <ObjectID xsi:nil="true" />
        <EmailAddress>hrajput@testing.com</EmailAddress>
        <Status>Active</Status></Results>
    <Results xsi:type="Subscriber">
        <PartnerKey xsi:nil="true" />
        <ObjectID xsi:nil="true" />
<EmailAddress>hrajput@testing.com</EmailAddress>
<Status>Active</Status></Results>
</RetrieveResponseMsg>
</soap:Body></soap:Envelope>

我正在尝试回复/存储上述响应中的EmailAddress,以便稍后在脚本中使用它。

我正在做的是:

$xmlstring = $client->__getLastResponse();

// Loads the XML
$xml = simplexml_load_string($xmlstring);

//print $xml->asXML();

echo "Result:".$xml->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->children('http://exacttarget.com/wsdl/partnerAPI')->Results[0]->EmailAddress;

但我一直收到这个错误:

  

致命错误:在

中的非对象上调用成员函数children()

我真的在努力抓住EmailAddress。我怎么能这样做?

2 个答案:

答案 0 :(得分:0)

尝试找出是否正在创建对象

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation

y = np.random.normal(0, 1, 1000000).cumsum(axis=0)
x = np.arange(y.size) + 1

fig, ax = plt.subplots()
line, = ax.plot([], [], 'k-')
ax.margins(0.05)

def init():
    line.set_data(x[:2],y[:2])
    return line,

def animate(i):
    win = 300
    imin = min(max(0, i - win), x.size - win)
    xdata = x[imin:i]
    ydata = y[imin:i]
    line.set_data(xdata, ydata)
    ax.relim()
    ax.autoscale()
    return line,

anim = animation.FuncAnimation(fig, animate, init_func=init, interval=25)

plt.show()

如果未创建对象。然后你可能需要检查你得到的XML。

答案 1 :(得分:0)

xml路径错误,您忘记了RetrieceResponseMsg。

正确的路径是:

$xml->children("http://schemas.xmlsoap.org/soap/envelope/")->Body->children("http://exacttarget.com/wsdl/partnerAPI")->RetrieveResponseMsg->Results->EmailAddress