使用简单XML PHP解析SOAP响应
在使用simplexml传递SOAP响应之后,我得到了以下输出。如何获取域ie、name和avail属性的值 使用的代码:使用简单XML PHP解析SOAP响应,php,Php,在使用simplexml传递SOAP响应之后,我得到了以下输出。如何获取域ie、name和avail属性的值 使用的代码: $xmlString = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $result); $xml = simplexml_load_string($xmlString); print_r($xml); SimpleXMLElement Object([soa
$xmlString = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $result);
$xml = simplexml_load_string($xmlString);
print_r($xml);
SimpleXMLElement Object([soapBody] => SimpleXMLElement Object ([CheckAvailabilityResponse] => SimpleXMLElement Object([CheckAvailabilityResult] => <?xml version="1.0" encoding="UTF-16"?>
<check>
<domain name="MYNAMEISNIJIL.COM" avail="1" canBackorder="0"/>
</check>) ))
$xmlString=preg_replace(“/(]*>)/”、“$1$2$3”、$result);
$xml=simplexml\u load\u字符串($xmlString);
打印(xml);
SimpleXMLElement对象([soapBody]=>SimpleXMLElement对象([CheckAvailabilityResponse]=>SimpleXMLElement对象([CheckAvailabilityResult]=>?xml version=“1.0”encoding=“UTF-16”?
检查
域名=“MYNAMEISNIJIL.COM”avail=“1”canBackorder=“0”/
/(检查)
显然,您在返回中转义了XML(这是一个糟糕的做法,我现在将忽略..)。另外,查看children()
函数以使用名称空间,而不是preg\u replace
。。。。忽略这一点,这将适用于您:
$outerxml = simplexml_load_string($xmlString);
$innerxml = simplexml_load_string( htmlspecialchars_decode(
$outerxml->soapBody->CheckAvailabilityResponse->CheckAvailabilityResult));
另一方面,我通常使用此小贴士来利用SOAPClient
解析soap响应:
//the soap way
class SneakyFauxSoap extends SoapClient {
public $response;
function __doRequest($val){
return $this->response;
}
}
$soap = new SneakyFauxSoap(null,
array(
'uri' =>'something',
'location'=>'something',
'soap_version' => SOAP_1_1));
$soap->response = $x;
var_dump($soap->somerandomfunction());
受@Wrikken答案的启发,我编写了一个简单易用的类 使用PHP5.3:
class SoapParser extends SoapClient {
private $xml;
function __construct($options) {
$options['location'] = $options['uri'] = 'dummy';
parent::__construct(null, $options);
}
public function __doRequest($request, $location, $action, $version,
$one_way = 0)
{
return $this->xml;
}
public function parse($xml) {
$this->xml = $xml;
return $this->dummyFunction();
}
}
用法示例:
$soapParser = new SoapParser(array('soap_version' => 'SOAP_1_1'));
try {
var_dump($soapParser->parse($response));
} catch (Exception $e) {
die($e->getMessage());
}