2016-11-28 25 views
0

我对于结合PHP使用XML表格很新颖。我试图从通过SOAP调用返回给我的XML文件中提取数据。从XML到PHP的特定字段

我的XML正在返回,因为这一点。

注释掉一些细节

<?xml version="1.0" encoding="UTF-8"?> 
<soapenv:Envelope  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"  xmlns="urn:partner.soap.sforce.com"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <soapenv:Body> 
     <loginResponse> 
      <result> 
       <metadataServerUrl>https://...-api.salesforce.com/service...</metadataServerUrl> 
       <passwordExpired>false</passwordExpired> 
       <sandbox>false</sandbox> 
       <serverUrl>https://...--api.salesforce.com/services/Soap/u/21.0/0...</serverUrl> 
       <sessionId>....</sessionId> 
       <userId>....</userId> 
       <userInfo> 
        <accessibilityMode>false</accessibilityMode> 
        <currencySymbol>€</currencySymbol>   ...   </userInfo> 
      </result> 
     </loginResponse> 
    </soapenv:Body> 
</soapenv:Envelope> 

所以我试图拉出来的这个会话ID

// UP HERE SOAP CALL --- return data 
....... 
} else { 
    $response = curl_exec($soap_do); 
    curl_close($soap_do); 
    // print($response); <-- see result XML 

    // grabbing the sessionid 
    $xmlresponse = new SimpleXMLElement($response);  
    $test = $xmlresponse->result->sessionId['value']; 
    echo $test;  
} 

这将返回空白,但是当我开始添加LoginResponse和Soapenv(身体和信封),我得到一个关于我想要获得非物件属性的错误。我不确定我在这里做错了什么。

+0

你为什么不使用'的file_get_contents()'? – SaidbakR

+0

没有这样工作,@SaidbakR。我通过卷发发送了一张表格,这是我回复的回复。 – Dorvalla

回答

3

使用SimpleXML可以使用SimpleXMLElement::children通过XML名称空间查找子项(此处为soapenv)。

对于您的情况下,它会像

$xmlresponse = new SimpleXMLElement($response);  
$response = $xmlresponse->children('soapenv', true)->Body->children('', true)->loginResponse->result->sessionId; 
var_dump($response); 

导致

object(SimpleXMLElement)#4 (1) { 
    [0]=> 
    string(4) "...." 
} 
+0

谢谢,这没有诀窍:) – Dorvalla

2

我想说的是,你应该使用SoapClient的(http://php.net/manual/tr/class.soapclient.php)为SOAP调用,但如果你不想使用它,这里是你如何解析这个XML:

$xmlresponse = new SimpleXMLElement(str_ireplace([':Envelope', ':Body'], '', $response));  
$test = $xmlresponse->soapenv->loginResponse->result->sessionId['value']; 
echo $test; 
+0

是的,我知道soapclient,我只是喜欢通过cURL做。并感谢您的输入 – Dorvalla