tags:

views:

60

answers:

1

I need to encode only part of the $delete path. Only 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!

The delete:

 $delete = "http://admin:[email protected]/@api/deki/DELETE:users/$user_id/properties/%s";
  1. Here you can see $user_id this will be an email address BUT the @ symbol needs to be encoded.
  2. The properties which follow at the very end, has a # within the name, this needs to also be encoded. For example, one property name *userprofile#external.created_date*

Here is the code so far:

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

    ?>
+1  A: 

Have you tried this? str_replace($string, array('@', '#'), array('%40', '%23'));

The urlencode function does not allow you to limit it to a subset of characters.

grmartin
No I haven't tried that. Where in the code exactly would I do this? And would it replace it in each of the properties as well? Thanks for your help.
Aaron
Well anywhere you need to escape those characters. so $user_id and where every the hash tag would be.
grmartin
Ok that makes sense. So I would just replace $string with the $user_id. For the property, would str_replace just go inside the for loop? I'm assuming I would just do the replace on the $name variable after it has been assigned. Let me know if that sounds right.
Aaron
Also, I tried using the str_replace right after the initial $user_id is defined. Then I simply printed the variable afterwards to see if it had done the replace but it had not. Is there something that I might be missing?
Aaron