2014-01-23 135 views
0

我正在使用php來使用Web服務(在Coldfusion中)來驗證活動目錄。我的代碼如下。解析PHP SimpleXMLElement

<?php 

$racf = $_SERVER['AUTH_USER']; 

//echo $racf; 
//echo $myracf = trim($racf, "FEDERATED\."); 


//get authenticated user 
$arrUser = explode("\\", $_SERVER["LOGON_USER"]); 
$racf = $arrUser[1]; 
echo $racf.'<br ><br >'; 

$logins = "http://acoldfusionwebservice/login.cfc?method=loginad&racf=$racf"; 

if(! $xml = simplexml_load_file($logins)) 
{ 
    echo 'unable to load XML file'; 
} 
else 
{ 
    //echo 'XML file loaded successfully <br />'; 
    print_r ($xml); 

} 
?> 

這產生了以下結果。

SimpleXMLElement Object 
(
    [@attributes] => Array 
     (
      [version] => 1.0 
     ) 

    [header] => SimpleXMLElement Object 
     (
     ) 

    [data] => SimpleXMLElement Object 
     (
      [recordset] => SimpleXMLElement Object 
       (
        [@attributes] => Array 
         (
          [rowCount] => 1 
          [fieldNames] => cn,mail,givenName,sn 
         ) 

        [field] => Array 
         (
          [0] => SimpleXMLElement Object 
           (
            [@attributes] => Array 
             (
              [name] => cn 
             ) 

            [string] => B000000 
           ) 

          [1] => SimpleXMLElement Object 
           (
            [@attributes] => Array 
             (
              [name] => mail 
             ) 

            [string] => [email protected] 
           ) 

          [2] => SimpleXMLElement Object 
           (
            [@attributes] => Array 
             (
              [name] => givenName 
             ) 

            [string] => John 
           ) 

          [3] => SimpleXMLElement Object 
           (
            [@attributes] => Array 
             (
              [name] => sn 
             ) 

            [string] => Doe 
           ) 
         ) 
       ) 
     ) 
) 

有人可以幫助我解析這些信息,以便我可以分配變量並使用它們。謝謝。

+2

這是你已經裝載到SimpleXML的XML。 YOu會像訪問simplexml一樣訪問它... $ xml-> data-> recordset-> field [0]將是cn/B0000節點 –

+0

您想訪問哪些數據? –

+0

我想訪問「B000000」,「[email protected]」,「John」和「Doe」。 – teej2542

回答

0

嘗試

$xml->data->recordset->field[0]->string 

我認爲那場[0]是一個對象再次

+0

這有效。謝謝你的幫助。 – teej2542