我从肥皂请求中获取响应,并将其传递给新的SimpleXML构造。
$response = $this->client->$method(array("Request" => $this->params));
$response_string = $this->client->__getLastResponse();
$this->response = new Classes_Result(new SimpleXMLElement($result));
如果我回显$ response_string,它会输出一个正确的xml字符串。这是一个片段,因为它很长。
<?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">
<soap:Body><GetClassesResponse xmlns="http://clients.mindbodyonline.com/api/0_5">
<GetClassesResult>
<Status>Success</Status>
<XMLDetail>Full</XMLDetail>
<ResultCount>6</ResultCount>
<CurrentPageIndex>0</CurrentPageIndex>
<TotalPageCount>1</TotalPageCount>
<Classes>
<Class>
<ClassScheduleID>4</ClassScheduleID>
<Location>
<SiteID>20853</SiteID>
....</soap:Envelope>
但是,当我尝试使用这个对象时,我会收到错误,或者如果我转储它输出的对象:
object(SimpleXMLElement)#51 (0)
为什么会发生这种情况的任何想法?
答案 0 :(得分:2)
您实际上并未使用$response_string
,并且未将$result
设置为new SimpleXMLElement($result)
。
也许您打算通过simplexml_load_string()
使用$response_string
字符串构建SimpleXML对象?
$response = $this->client->$method(array("Request" => $this->params));
$response_string = $this->client->__getLastResponse();
// Load XML via simplexml_load_string()
$this->response = new Classes_Result(simplexml_load_string($response_string));
// Or if you do expect a SimpleXMLElement(), pass in the string
$this->response = new Classes_Result(new SimpleXMLElement($response_string));
SOAP响应的<soap:Body>
元素使用(soap
)命名空间。要使用SimpleXML循环它,您必须提供正确的命名空间:
// After creating new SimpleXMLElement()
var_dump($this->response->children("http://schemas.xmlsoap.org/soap/envelope/"));
// class SimpleXMLElement#2 (1) {
// public $Body =>
// class SimpleXMLElement#4 (0) {
// }
// }
循环身体:
foreach ($this->response->children("http://schemas.xmlsoap.org/soap/envelope/") as $b) {
$body_nodes = $b->children();
// Get somethign specific
foreach ($body_nodes->GetClassesResponse->GetClassesResult as $bn) {
echo $bn->ResultCount . ", ";
echo $bn->TotalPageCount;
}
}
// 6, 1