Posted By

browncardigan on 02/17/10


Tagged

twitter


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

dantreacy
nb109


unfollow all your followers on Twitter


 / Published in: PHP
 

  1. define("USERNAME", "__YOU_TWITTER_USER_NAME_HERE__");
  2. define("PASSWORD", "__YOU_TWITTER_PASSWORD_HERE__");
  3.  
  4. // 1. Validate your credentials
  5. $validate = api("http://twitter.com/account/verify_credentials.xml");
  6. if ($validate->error) {
  7. die("<strong>Error: </strong>" . $validate->error);
  8. }
  9.  
  10. // 2. Get a list of your friends IDs
  11. $friends = simplexml2array(api("http://twitter.com/friends/ids.xml"));
  12. if (isset($friends['id'])) {
  13. $friends = $friends['id'];
  14. }
  15. else {
  16. echo "Error getting friends!";
  17. }
  18.  
  19. // 3. Unfollow everyone!
  20. if (is_array($friends) && count($friends) > 0) {
  21. foreach($friends as $f){
  22. $xml = api("http://twitter.com/friendships/destroy/" . $f . ".xml", "DELETE");
  23. if ($error = $xml->error) {
  24. echo "<b>ERROR:</b> " . $f . "(" . $error. ")<br />";
  25. }
  26. else {
  27. echo "Successfully unfollowed: " . $f . "<br />";
  28. }
  29. flush();
  30. }
  31. }
  32.  
  33. // **************************************************
  34. // Functions
  35.  
  36. // convert SimpleXML Object into an array
  37. function simplexml2array($xml) {
  38. if (get_class($xml) == 'SimpleXMLElement') {
  39. $attributes = $xml->attributes();
  40. foreach($attributes as $k=>$v) {
  41. if ($v) $a[$k] = (string) $v;
  42. }
  43. $x = $xml;
  44. $xml = get_object_vars($xml);
  45. }
  46. if (is_array($xml)) {
  47. if (count($xml) == 0) return (string) $x; // for CDATA
  48. foreach($xml as $key=>$value) {
  49. $r[$key] = simplexml2array($value);
  50. }
  51. if (isset($a)) $r['@'] = $a; // Attributes
  52. return $r;
  53. }
  54. return (string) $xml;
  55. }
  56.  
  57. // Calls Twitter API and returns SimpleXML Object
  58. function api($url, $method="GET"){
  59. $ch = curl_init();
  60. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
  61. curl_setopt($ch, CURLOPT_URL, $url);
  62. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  63. curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  64. curl_setopt($ch, CURLOPT_USERPWD, USERNAME.":".PASSWORD);
  65. $buffer = curl_exec($ch);
  66. curl_close($ch);
  67. return(simplexml_load_string($buffer));
  68. }

Report this snippet  

You need to login to post a comment.