2010-10-13 93 views
0

我已經創建了一個腳本來刪除特定個人的所有用戶屬性。我能夠使用api調用從xml獲取用戶的屬性。我正在使用刪除api來刪除每個屬性。測試什麼時候沒有屬性

我希望能夠測試什麼時候沒有更多的屬性,然後相應地輸出一條消息。在for循環中找到每個屬性的位置。任何幫助肯定是讚賞的。

下面是代碼:

<?php 

$user_id="[email protected]"; 

$url=('http://user:[email protected]/@api/users/[email protected]/properties'); 
$xmlString=file_get_contents($url); 

$delete = "http://user:[email protected]/@api/DELETE:users/$user_id/properties/%s"; 
$xml = new SimpleXMLElement($xmlString); 

function curl_fetch($url,$username,$password,$method='DELETE') 
{ 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); // returns output as a string instead of echoing it 
    curl_setopt($ch,CURLOPT_USERPWD,"$username:$password"); // if your server requires basic auth do this 
    return curl_exec($ch); 
} 

foreach($xml->property as $property) { 
    $name = $property['name']; // the name is stored in the attribute 
    curl_fetch(sprintf($delete, $name),'user','12345'); 
} 

?> 

回答

0

foreach構建自動循環,直到可枚舉對象(即$xml)的端部。

我認爲你正在尋找的count功能:

if(!count($xml->property)) die('No more properties'); 
+0

感謝,我想這就是我需要的 – Aaron 2010-10-13 14:37:33

相關問題