我正在嘗試創(chuàng)建一個腳本,刪除特定個人的所有用戶屬性.我可以使用api調(diào)用來獲取用戶的屬性.我正在嘗試使用刪除api刪除每個屬性.但我有一個問題.以下是代碼:
$delete = "http://www./@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來執(zhí)行實際刪除.以下是該命令的示例(property = something):
curl -u username:password -X DELETE -i http:///@api/users/=john_smith@/properties/something
-u提供外部用戶身份驗證.
-X指定HTTP請求方法.
-i輸出HTTP響應(yīng)頭.對調(diào)試很有用.
這是我可以合并到現(xiàn)有腳本中的東西嗎?或者我還需要做些什么嗎?任何幫助將不勝感激.
更新:
<?php
$user_id="john_smith@";
$url=('http://aaron:12345@192.168.245.133/@api/deki/users/=john_smith@/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');
}
?>
解決方法: 您可以使用php curl,或使用exec外殼卷曲.
如果您的Web服務(wù)器上已經(jīng)啟用了curl,請使用php curl.如果你不能安裝php-curl復(fù)制curl的命令行版本,你很高興.
在php-curl中設(shè)置delete方法:
curl_setopt($ch,CURLOPT_CUSTOMREQUEST,’DELETE’);
編輯
像這樣的東西:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www./@api/whatever/url/you/want/or/need");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
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
$output = curl_exec($ch);
EDIT2
在函數(shù)中堅持上面的代碼:
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);
}
并使用新函數(shù)替換腳本中對file_get_contents()的調(diào)用.
curl_fetch(sprintf($delete,$name),’aaron’,’12345′);
完成. 來源:https://www./content-1-300351.html
|