2016-01-18 107 views
0

我已經弄清楚如何在PHP中使用JSS Casper API,並在XML中接收響應。在瀏覽器中,它只是格式化一行而沒有任何空格。這裏是一個XML示例:如何格式化XML?

<accounts> 
    <users> 
    <user> 
     <id>1</id> 
     <name>administrator</name> 
    </user> 
    </users> 
    <groups> 
    <group> 
     <id>2</id> 
     <name>Example Group</name> 
     <site> 
     <id>-1</id> 
     <name>None</name> 
     </site> 
    </group> 
    </groups> 
</accounts> 

如何將其格式化爲HTML表格或更漂亮的東西?有沒有簡單的方法來轉換它?

這裏是我試過的代碼:

<?php 
    $curl = curl_init(); 
    curl_setopt($curl, CURLOPT_URL, "https://example.jamfcloud.com/JSSResource/accounts"); 
    curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
    curl_setopt($curl, CURLOPT_USERPWD, "user:pass"); 
    $result = curl_exec($curl); 
    curl_close($curl); 

    $arr = simplexml_load_string_($curl); 
    $xml = $arr->member; 
?> 
<table> 
    <tr> 
     <th>User</th> 
     <th>ID</th> 
     <th>Name</th> 
    </tr> 
    <?php foreach($xml as $row) { ?> 
     <tr> 
      <td><?php echo $row->user ?></td> 
      <td><?php echo $row->id ?></td> 
      <td><?php echo $row->name ?></td> 
     </tr> 
    <?php } ?> 
</table> 
+0

這裏是[例子](https://gist.github.com/betweenbrain/5405671)你可以試試。 – usandfriends

回答

1

要調用simplexml_load_string錯誤:

$arr = simplexml_load_string_($curl); 

實際上應該是:

$arr = simplexml_load_string($result); 


刪除

$xml = $arr->member; 

,因爲你只需更換foreach循環,如:

<?php foreach($arr->users->user as $row) { ?> 

this answer