4

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

?>

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

9 Comments

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.
Where do the url and credentials belong? And how do I actually call the delete method?
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 You need to put the whole chunk of code I posted in the loop.
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?
|
0

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

2 Comments

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?
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.
0

See: http://php.net/curl

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.