views:

27

answers:

1

I'm using the following code to find all properties for a user and in turn delete them. My problem is that I'm getting a warning: Warning: sprintf(): Too few arguments for each of the properties.

However, when I manually enter the $user_id for the delete string as first_last%%40ourwiki.com it works!

Seems like sprintf requires double '%' but not sure why. Is there a way to get around this? Also, I'm using the same variable for file_get_contents and this works fine.

The Code:

$user="[email protected]";
$user_id=str_replace(array('@', '#'), array('%40', '%23'), $user);
print $user_id;

$url=("http://admin:[email protected]/@api/users/=$user_id/properties");
$xmlString=file_get_contents($url);

$delete = "http://admin:[email protected]/@api/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);
    curl_setopt($ch,CURLOPT_USERPWD,"$username:$password");
    return  curl_exec($ch);
}

foreach($xml->property as $property) {
  $name = $property['name'];
  $name2=str_replace(array('@', '#'), array('%40', '%23'), $name);
  print $name2;
  curl_fetch(sprintf($delete, $name2),'admin','password');
}

Thanks in advance!

+1  A: 

% is a special character in sprintf(). So you have to escape all % before processing it, %% is a literal %s.

$delete = str_replace("http://admin:[email protected]/@api/users/=$user_id/properties/", '%', '%%').'%s';

You do not have to use sprintf here, you can use the concatenation operator too, like:

$delete = "http://admin:[email protected]/@api/users/=$user_id/properties/";
curl_fetch( $delete . $name2, 'admin', 'password' );
Lekensteyn
Thank you very much, that seems to be working for me :)
Aaron