在脚本中使用 urlencode

发布于 2024-09-18 15:00:41 字数 1752 浏览 3 评论 0原文

我一直在开发一个可用于内部 wiki 的脚本,该脚本将删除不活动用户的属性。我相信我已经快到了,但是 api 遇到了问题。

我想我需要在 $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');
    }

    ?>

I've been working on a script that can be used for an internal wiki, that will remove the properties for a user who is inactive. I believe I'm almost there but having a problem with the api.

I think I need to use urlencode on the $delete path BUT only for the @ in the email address and # in the property. I know how to use urlencode for the whole thing but not on just that. The way it works, is it loops through to get the properties and most of them include # in the name. Anyone who can help modify so that this works would be greatly appreciated!

Here is the script:

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

    ?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撑一把青伞 2024-09-25 15:00:41

像这样?

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

Like this?

$delete = "http://admin:[email protected]/@api/deki/DELETE:users/".urlencode($user_id)."/properties/%s";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文