Using urlencode in script
I've been working on a script that can be used for an internal wiki, that will remove the properties for a user who is inactive. I believe I'm almost there but having a problem with the api.
I think I need to use urlencode on the $delete path BUT only for the @ in the email address and # in the property. I know how to use urlencode for the whole thing but not on just that. The way it works, is it loops through to get the properties and most of them include # in the name. Anyone who can help modify so that this works would be greatly appreciated!
Here is the script:
<?php
$user_id="john_smith@ourwiki.com";
$url=('http://admin:12345@192.168.245.133/@api/deki/users/=john_smith@ourwiki.com/properties');
$xmlString=file_get_contents($url);
$delete = "http://admin:12345@192.168.245.133/@api/deki/DELETE:users/$user_id/properties/%s";
开发者_StackOverflow社区 $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),'admin','12345');
}
?>
Like this?
$delete = "http://admin:12345@192.168.245.133/@api/deki/DELETE:users/".urlencode($user_id)."/properties/%s";
精彩评论