在脚本中使用urlencode

问题描述:

我一直在研究可用于内部wiki的脚本,该脚本将删除处于非活动状态的用户的属性。我相信我快到了,但是api有问题。在脚本中使用urlencode

我认为我需要在$ delete路径上使用urlencode但是只能用于电子邮件地址中的@和属性中的#号。我知道如何将urlencode用于整个事情,但不是那样。它的工作方式是通过循环来获取属性,其中大部分都包含名称中的#号。任何人可以帮助修改,以便这个作品将不胜感激!

下面是脚本:

<?php 

    $user_id="[email protected]"; 

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

    $delete = "http://admin:[email protected]/@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),'admin','12345'); 
    } 

    ?> 
+0

此行是否有效?:'$ xmlString = file_get_contents($ url);'。如果是,则该URL有效。 – 2010-09-12 19:01:24

+0

你不能只使用str_replace,如果你知道你想要编码成什么? – 2010-09-12 19:07:17

+0

@SimpleCoder它确实存在,但问题是删除api不起作用。当我使用urlencoded版本运行单独的CURL命令时,它会根据需要删除属性。但是,当它没有编码时,它给了我一个404错误,无法完成。 – Aaron 2010-09-12 19:08:50

喜欢这个?

$delete = "http://admin:[email protected]/@api/deki/DELETE:users/".urlencode($user_id)."/properties/%s";