Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 当没有属性时进行测试_Php_Xml_Curl - Fatal编程技术网

Php 当没有属性时进行测试

Php 当没有属性时进行测试,php,xml,curl,Php,Xml,Curl,我已经创建了一个脚本,该脚本将删除特定个人的所有用户属性。我能够使用api调用从xml中获取用户的属性。我使用deleteapi删除每个属性 我希望能够在没有更多属性时进行测试,然后相应地输出一条消息。在for循环中可以找到每个属性。我们当然感谢您的帮助 代码如下: <?php $user_id="john_smith@ourwiki.com"; $url=('http://user:12345@192.168.245.133/@api/users/=john_smith@ourwik

我已经创建了一个脚本,该脚本将删除特定个人的所有用户属性。我能够使用api调用从xml中获取用户的属性。我使用deleteapi删除每个属性

我希望能够在没有更多属性时进行测试,然后相应地输出一条消息。在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');
}

?>
属性作为$property){
$name=$property['name'];//名称存储在属性中
curl_fetch(sprintf($delete,$name),'user','12345');
}
?>

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

我认为您正在寻找以下功能:

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