Sunday, July 25, 2010

Problems deleting Direct Messages from twitter using PHP/Curl and the Supertweet.net api

Basically my problem is outlined above. I am trying to delete direct messages using the message id - using php and the supertweet.api - I've looked at the twitter API wiki have not found what I am doing wrong.

This is the response I get back:

The method specified in the request is not allowed for the resource identified by the request URI You can get technical details http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.6

Going to the w3.org web site this is what it says:

10.4.6 405 Method Not Allowed

The method specified in the Request-Line is not allowed for the resource identified by the Request-URI. The response MUST include an Allow header containing a list of valid methods for the requested resource.

Which to me isn't helpful.

My code is this:

$msgdestroy = "http://api.supertweet.net/1/direct_messages/destroy/";
$msgidid = $checksum3[$counter1] . '.xml'; //this is the id of the message.

function distroydm($destroyurl,$user,$password, $msgidid) {
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, "$destroyurl");
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "$msgidid");
curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($curl_handle, CURLOPT_USERPWD, "$user:$password");
$buffer=curl_exec($curl_handle);
curl_close($curl_handle);

return $buffer;
}

The twitter API says, to do this:
curl -u user:password --http-request DELETE http://api.twitter.com/1/direct_messages/destroy/88619848.xml

Which would be a command line, of the above code (I think), when I run it via the command line, however - I get the same error directing me back to w3.org

I've googled this, and didn't find anything helpful - Hoping someone who understands the API a bit better maybe able to give me some idea as to what I am doing wrong.

-Thanks - LeRoy, KD8BXP

3 comments:

  1. Initially the proxy implemented only GET and POST methods. We added DELETE today. Let us know if this code works now.

    ReplyDelete
  2. Thanks,
    I just tried it, now getting the error:
    1/direct_messages/destroy/
    Could not authenticate with OAuth.
    I looked at the google group, but didn't see anything, which I thought I saw something about this the other day.

    ReplyDelete
  3. @Mr Blog - IT works, I changed one line in my code, and worked fine! - I am posting the relevant code here, as it was very hard for me to find an example of how to do this - The supertweet/twitter api needs to POST and DELETE to delete a direct message - The post part was easy enough to find...but DELETE...ok that took a while.
    Anyways, this is what works for me:

    Written in PHP:
    function distroydm($destroyurl,$user,$password, $msgidid) {
    $curl_handle = curl_init();
    curl_setopt($curl_handle, CURLOPT_URL, "$destroyurl");
    curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl_handle, CURLOPT_POST, 1);
    curl_setopt($curl_handle, CURLOPT_CUSTOMREQUEST, "DELETE");
    curl_setopt($curl_handle, CURLOPT_USERPWD, "$user:$password");
    $buffer=curl_exec($curl_handle);
    curl_close($curl_handle);

    return $buffer;
    }

    ReplyDelete