【发布时间】:2011-04-16 08:15:35
【问题描述】:
我只需要对 $delete 路径的一部分进行编码。只有电子邮件地址中的 @ 和属性中的 #。我知道如何将 urlencode 用于整个事情,但不仅限于此。它的工作方式是循环获取属性,其中大多数在名称中包含#。任何可以帮助修改以使其工作的人将不胜感激!
删除:
$delete = "http://admin:12345@192.168.245.133/@api/deki/DELETE:users/$user_id/properties/%s";
- 在这里您可以看到 $user_id 这将是一个电子邮件地址,但需要对 @ 符号进行编码。
- 最后的属性名称中有一个#,这也需要编码。例如,一个属性名称 userprofile#external.created_date
这是目前为止的代码:
<?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";
$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');
}
?>
【问题讨论】:
-
请精简您的示例代码以仅针对您遇到的问题。我无法真正确定您要转义哪些数据或将其用于何处。
-
@erisco 感谢您对此的回复。我很抱歉没有说得这么清楚。我编辑了帖子并试图更好地记录需要修复的内容。这一切都与现有的 api 有关。我有一个 for 循环正在运行以捕获特定 user_id 的所有给定属性。如果您还有问题,请告诉我。