我正在使用第三方Web服务发布请求并使用php curl通过soap获得响应。响应很好但由于某种原因我无法解析响应。当我使用simplexml_load_string时它给我空白。
我的代码是
// PHP cURL for https connection with auth
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $soapUser.":".$soapPassword); // username and password - declared at the top of the doc
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_post_string); // the SOAP request
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// converting
$response = curl_exec($ch);
print_r($response); // when I print response it shows me the correct response with data.
but when I am doing ,
$parser = simplexml_load_string($response2);
print_r($parser); // it's giving SimpleXMLElement Object ( )
标题是
$headers = array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: application/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"Content-length: ".strlen($xml_post_string),
);
soap标头是
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ws="http://ws.mrted.com/">
<soapenv:Header>
<wsse:Security soapenv:mustUnderstand="1" 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">
<wsse:UsernameToken wsu:Id="UsernameToken-1">
<wsse:Username>*********************:guest:FO</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">guest</wsse:Password>
<wsu:Created>2012-07-09T11:35:20.019Z</wsu:Created>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
我得到的回复是在浏览器中打印卷曲响应后
<env:envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:header></env:header>
<env:body>
<ns2:getcriteriaresponse xmlns:ns2="http://ws.mrted.com/">
<ns2:standardcriteriawithlovs>
<adlanguages><language><label>English (UK)</label><value>UK</value></language></adlanguages>
<countries><country><label>Australia</label><value>1116</value><regions><region><label>Australian Capital Territory</label><value>3027</value></region><region><label>New South Wales</label><value>3028</value></region><region><label>Northern Territory</label><value>3029</value></region><region><label>Queensland</label><value>3030</value></region><region><label>South Australia</label><value>3031</value></region><region><label>Tasmania</label><value>3032</value></region><region><label>Victoria</label><value>3033</value></region><region><label>Western Australia</label><value>3034</value></region></regions></country><country><label>United Kingdom</label><value>1290</value><regions><region><label>East Anglia</label><value>3429</value></region><region><label>East Midlands</label><value>3430</value></region><region><label>England</label><value>4329</value></region><region><label>London</label><value>3431</value></region><region><label>Midlands</label><value>3432</value></region><region><label>North East</label><value>3433</value></region><region><label>North West</label><value>3434</value></region><region><label>Northern Ireland</label><value>3435</value></region><region><label>Scotland</label><value>3436</value></region><region><label>South</label><value>4332</value></region><region><label>South East</label><value>3437</value></region><region><label>South West</label><value>3438</value></region><region><label>Wales</label><value>3439</value></region><region><label>West Midlands</label><value>3440</value></region></regions></country></countries>
</ns2:standardcriteriawithlovs>
</ns2:getcriteriaresponse>
</env:body>
</env:envelope>
可以肯定的是,我正在获取数据,但格式可能有误,或者我遗漏了一些东西。
请提出建议。非常感谢。
答案 0 :(得分:2)
$response2 = <<<EOF
<env:envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:header></env:header>
<env:body>
<ns2:getcriteriaresponse xmlns:ns2="http://ws.mrted.com/">
<ns2:standardcriteriawithlovs>
<adlanguages>
<language>
<label>English (UK)</label>
<value>UK</value>
</language>
</adlanguages>
</ns2:standardcriteriawithlovs>
</ns2:getcriteriaresponse>
</env:body>
</env:envelope>
EOF;
$parser = simplexml_load_string($response2);
$parserEnv = $parser->children('env', true);
$languages = $parserEnv->body->children('ns2', true)
->getcriteriaresponse->children('ns2', true)
->standardcriteriawithlovs->children()
->adlanguages->children();
foreach($languages as $language) {
$label = (string) $language->label;
$value = (string) $language->value;
echo $label, ' => ', $value, PHP_EOL;
}