2010-10-02 162 views
0

我正在使用以下代码来查找用户的所有属性,然后删除它们。我的问题是,我收到警告:警告:sprintf():每个属性的参数太少sprintf警告 - 编码问题

然而,当我手动输入$ USER_ID进行删除字符串作为FIRST_LAST %% 40ourwiki.com它的作品!

似乎像sprintf需要双'%'但不知道为什么。有没有办法解决这个问题?此外,我使用file_get_contents相同的变量,这工作正常。

验证码:

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

提前感谢!

回答

0

%sprintf()中的特殊字符。所以你必须在处理它之前逃脱所有%%%是一个文字%s

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

你不必在这里使用sprintf,您可以使用连接运算符太像:

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

非常感谢你,这似乎是为我工作:) – Aaron 2010-10-02 19:26:36