【发布时间】:2011-04-05 22:06:06
【问题描述】:
我正在尝试创建一个脚本,该脚本将删除特定个人的所有用户属性。我可以使用 api 调用来获取用户的属性。我正在尝试使用删除 api 来删除每个属性。但我这样做有问题。下面是代码:
$delete = "http://www.ourwiki.com/@api/DELETE:users/$user_id/properties/%s";
$xml = new SimpleXMLElement($xmlString);
foreach($xml->property as $property) {
$name = $property['name']; // the name is stored in the attribute
file_get_contents(sprintf($delete, $name));
}
我相信我需要使用 curl 来执行实际的删除。以下是该命令的示例(property=something):
curl -u username:password -X DELETE -i http://ourwiki.com/@api/users/=john_smith@ourwiki.com/properties/something
-u 提供外部用户认证。
-X 指定HTTP请求方法。
-i 输出 HTTP 响应标头。对调试很有用。
这是我可以直接合并到现有脚本中的东西吗?或者还有什么我需要做的吗?任何帮助将不胜感激。
更新:
<?php
$user_id="john_smith@ourwiki.com";
$url=('http://aaron:12345@192.168.245.133/@api/deki/users/=john_smith@ourwiki.com/properties');
$xmlString=file_get_contents($url);
$delete = "http://aaron:12345@192.168.245.133/@api/deki/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),'aaron','12345');
}
?>
【问题讨论】: