2010-10-02 8 views
0

J'utilise le code suivant pour trouver toutes les propriétés d'un utilisateur et les supprimer à leur tour. Mon problème est que j'obtiens un avertissement: Avertissement: sprintf(): trop peu d'arguments pour chacune des propriétés.sprintf warning - encoding issue

Cependant, quand je rentre manuellement $ user_id pour la chaîne de suppression comme first_last %% 40ourwiki.com ça marche!

On dirait que sprintf nécessite le double '%', mais je ne sais pas pourquoi. Y a-t-il un moyen de contourner ceci? En outre, j'utilise la même variable pour file_get_contents et cela fonctionne très bien.

Le 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'); 
} 

Merci à l'avance!

Répondre

0

% est un caractère spécial dans sprintf(). Donc, vous devez échapper à tous % avant de le traiter, %% est un littéral %s.

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

Vous ne devez pas utiliser sprintf ici, vous pouvez utiliser l'opérateur de concaténation aussi, comme:

$delete = "http://admin:[email protected]/@api/users/=$user_id/properties/"; 
curl_fetch($delete . $name2, 'admin', 'password'); 
+0

Merci beaucoup, qui semble fonctionner pour moi :) – Aaron