tags:

views:

86

answers:

3

I'm trying to create a script that will delete all user properties for a particular individual. I'm able to use an api call to get the users' properties. And I'm trying use a delete api to remove each property. But I'm having an issue doing so. Below is the code:

$delete = "http://www.ourwiki.com/@api/DELETE:users/$user_id/properties/%s";
$xml = new SimpleXMLElement($xmlString);

foreach($xml->property as $property) {
  $name = $property['name']; // the name is stored in the attribute
  file_get_contents(sprintf($delete, $name));
}

I believe I need to use curl to perform the actual delete. Here is an example of that command (property=something):

curl -u username:password -X DELETE -i http://ourwiki.com/@api/users/[email protected]/properties/something

-u Provides external user authentication.

-X Specifies the HTTP request method.

-i Outputs the HTTP response headers. Useful for debugging.

Is this something that I can incorporate right into the existing script? Or is there something else I need to do? Any help would be greatly appreciated.

update:

<?php

$user_id="[email protected]";

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

$delete = "http://aaron:[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),'aaron','12345');
}

?>
+2  A: 

You can use php curl, or shell out to curl using exec.

If curl is already enabled on you web server, go with php curl. If you cant install php-curl copy a command line version of curl and you are good to go.

In php-curl to set the delete method do:

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');

edit

Something like this:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.ourwiki.com/@api/whatever/url/you/want/or/need");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
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
$output = curl_exec($ch);

edit2

stick that above code in a function:

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

and replace the call to file_get_contents() in your script with the new function.

curl_fetch(sprintf($delete, $name),'aaron','12345');

Done.

Byron Whitlock
When I run phpinfo it appears that curl is enabled. So does this mean I can simply run that command within the php script. If so, how is that actually done? I'm not very familiar with curl unfortunately.
Aaron
Where do the url and credentials belong? And how do I actually call the delete method?
Aaron
Thank you very much! That helps a lot. Does this mean that I need to call $ch instead of $delete from within the for loop?
Aaron
@Aaron You need to put the whole chunk of code I posted in the loop.
Byron Whitlock
Ok, I thought that was the case. I just wasn't sure what to do with my existing delete. Since it is in the file_get_contents and would be using the current api "delete" call not the curl one. Do I need to make any other changes?
Aaron
And you better accept my answer. 40% is abysmal and a no no on SO.
Byron Whitlock
Don't worry I accepted :) I still do seem to be having an issue somewhere. Can you check out my update above? I've included everything I have so far. When I run the script, I'm not getting any errors now. However when I manually run the api call to get the users properties everything still appears there and has not been deleted. Any ideas why this might be happening? Also, I did see in a log file that it tells me ProcessRequest: feature not found(DELETE URI: http://localhost:8081/deki/DELETE:users/[email protected]/properties/ourwiki.userprofile) Is this a syntax issue possibly?
Aaron
The second and third parameters to the delete function shoud be the password for the webservice. I just put aaron 1234 as a sample. if the webservice doesn't take a username or password, comment out the liine in the function. if they do, put in hte correct user/pass.
Byron Whitlock
I thought that was the case and I used my credentials in place for the user/pass but seems something else is still going on :(
Aaron
Looks like it was having difficulty because the @ in the email and # in the property needed to be encoded. How would I modify the script so that the properties use %23 each time it loops through? Thanks for your help
Aaron
A: 

looks like you are looking for the curl function calls in php, including curl_setopt

Zak
I looked up curl_setopt, I'm just not exactly sure how to run the command based on my example. Like how do I supply the URL and the credentials?
Aaron
Byron did a good job outlining it.
Zak
Thanks, any chance you know how i would properly call delete? As of now my for loop is using the old api call for $delete. I'm just not sure how to use the code that he provided in my example.
Aaron
A: 

See: http://php.net/curl

JackFuchs