【问题标题】:Test when there are no properties没有属性时进行测试
【发布时间】:2010-10-13 04:13:51
【问题描述】:

我创建了一个脚本,它将删除特定个人的所有用户属性。我可以使用 api 调用从 xml 中获取用户的属性。我正在使用删除 api 来删除每个属性。

我希望能够在没有更多属性时进行测试,然后相应地输出一条消息。在 for 循环中可以找到每个属性。任何帮助都将不胜感激。

下面是代码:

<?php

$user_id="john_smith@ourwiki.com";

$url=('http://user:12345@192.168.245.133/@api/users/=john_smith@ourwiki.com/properties');
$xmlString=file_get_contents($url);

$delete = "http://user:12345@192.168.245.133/@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');
}

?>

【问题讨论】:

    标签: php xml curl


    【解决方案1】:

    foreach 构造会自动循环,直到可枚举对象(即$xml)结束。

    我认为您正在寻找 count 函数:

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

    【讨论】:

      猜你喜欢
      • 2021-06-19
      • 1970-01-01
      • 1970-01-01
      • 2017-09-11
      • 2020-08-29
      • 2020-12-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多