2013-08-29 271 views
0

我正在使用第三方Web服务发布请求并通过使用php curl的soap获得响应。响应很好,但由于某种原因我无法解析响应。当我使用simplexml_load_string时给我空白。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> 

这是肯定的,我得到的数据,但格式可能是错误的或我缺少的东西。

请提前。多谢。

+1

这是因为SimpleXML不返回数组,它返回嵌套的SimpleXML元素对象集合;你需要加载它,然后解析它,找到你需要的东西,然后可以转换为字符串/整数/浮点数/等 –

+0

感谢Mark.Do你有任何参考手册如何加载,然后解析请。 –

+1

http://www.php.net/manual/en/simplexml.examples-basic.php是一个很好的网页开始 –

回答

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; 
} 
+0

从现在开始,你是我的上师。这只是一段精彩的编码。非常感谢你。最后一件事是,如果我把这些国家当作'standardcriteriawithlovs'的子女,并且在我拥有两个国家的国家下,我想选择第二个国家的所有地区(答复在我的文章)然后将它是$ languages = $ parserEnv-> Body-> children('ns2',true) - > getCriteriaResponse-> children('ns2',true ) - > standardCriteriaWithLovs-> children() - > countries-> children() - > country [1] - > children(); ? –

+1

我会使用getName()方法循环访问'standardcriteriawithlovs-> children()'来测试每个孩子是否是一种语言或一个国家,然后遍历每个孩子的孩子以获得单独的语言或国家详细信息;但只要您知道各个国家将始终在那里 –

+0

让我尝试使用getName()方法,您就可以使用您的直接方法。 –