Return to Snippet

Revision: 23980
at February 17, 2010 23:31 by browncardigan


Updated Code
define("USERNAME", "__YOU_TWITTER_USER_NAME_HERE__");
define("PASSWORD", "__YOU_TWITTER_PASSWORD_HERE__");

// 1. Validate your credentials
$validate = api("http://twitter.com/account/verify_credentials.xml");
if ($validate->error) {
	die("<strong>Error: </strong>" . $validate->error);
}

// 2. Get a list of your friends IDs
$friends = simplexml2array(api("http://twitter.com/friends/ids.xml"));
if (isset($friends['id'])) {
	$friends = $friends['id'];
}
else {
	echo "Error getting friends!";
	exit;
}

// 3. Unfollow everyone!
if (is_array($friends) && count($friends) > 0) {
	foreach($friends as $f){
	    $xml = api("http://twitter.com/friendships/destroy/" . $f . ".xml", "DELETE");
	    if ($error = $xml->error) {
	        echo "<b>ERROR:</b> " . $f . "(" . $error. ")<br />";
	    }
		else {
			echo "Successfully unfollowed: " .  $f . "<br />";
	    }
		flush();
	}
}

// **************************************************
// Functions

// convert SimpleXML Object into an array
function simplexml2array($xml) {
   if (get_class($xml) == 'SimpleXMLElement') {
       $attributes = $xml->attributes();
       foreach($attributes as $k=>$v) {
           if ($v) $a[$k] = (string) $v;
       }
       $x = $xml;
       $xml = get_object_vars($xml);
   }
   if (is_array($xml)) {
       if (count($xml) == 0) return (string) $x; // for CDATA
       foreach($xml as $key=>$value) {
           $r[$key] = simplexml2array($value);
       }
       if (isset($a)) $r['@'] = $a;    // Attributes
       return $r;
   }
   return (string) $xml;
}

// Calls Twitter API and returns SimpleXML Object
function api($url, $method="GET"){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($ch, CURLOPT_USERPWD, USERNAME.":".PASSWORD);
    $buffer = curl_exec($ch);
    curl_close($ch);
    return(simplexml_load_string($buffer));
}

Revision: 23979
at February 17, 2010 23:28 by browncardigan


Updated Code
define("USERNAME", "__YOU_TWITTER_USER_NAME_HERE__");
define("PASSWORD", "__YOU_TWITTER_PASSWORD_HERE__");

// 1. Validate your credentials
$validate = api("http://twitter.com/account/verify_credentials.xml");
if ($validate->error) {
	die("<strong>Error: </strong>" . $validate->error);
}

// 2. Get a list of your friends IDs
$friends = simplexml2array(api("http://twitter.com/friends/ids.xml"));
if (isset($friends['id'])) {
	$friends = $friends['id'];
}
else {
	echo "Error getting friends!";
	exit;
}

// 3. Unfollow everyone!
if (is_array($friends) && count($friends) > 0) {
	foreach($friends as $f){
	    $xml = api("http://twitter.com/friendships/destroy/" . $f . ".xml", "DELETE");
	    if ($error = $xml->error) {
	        echo "<b>ERROR:</b> " . $f . "(" . $error. ")<br />";
	    }
		else {
			echo "Successfully unfollowed: " .  $f . "<br />";
	    }
		flush();
	}
}

// **************************************************
// Functions

// Put all SimpleXML Object values into array so we can parse them with PHP's array tools
function simplexml2array($xml) {
   if (get_class($xml) == 'SimpleXMLElement') {
       $attributes = $xml->attributes();
       foreach($attributes as $k=>$v) {
           if ($v) $a[$k] = (string) $v;
       }
       $x = $xml;
       $xml = get_object_vars($xml);
   }
   if (is_array($xml)) {
       if (count($xml) == 0) return (string) $x; // for CDATA
       foreach($xml as $key=>$value) {
           $r[$key] = simplexml2array($value);
       }
       if (isset($a)) $r['@'] = $a;    // Attributes
       return $r;
   }
   return (string) $xml;
}

// Calls Twitter API and returns SimpleXML Object
function api($url, $method="GET"){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($ch, CURLOPT_USERPWD, USERNAME.":".PASSWORD);
    $buffer = curl_exec($ch);
    curl_close($ch);
    return(simplexml_load_string($buffer));
}

Revision: 23978
at February 17, 2010 23:27 by browncardigan


Initial Code
define("USERNAME", "__YOU_TWITTER_USER_NAME_HERE__");
define("PASSWORD", "__YOU_TWITTER_PASSWORD_HERE__");

// 1. Validate your credentials
$validate = api("http://twitter.com/account/verify_credentials.xml");
if ($validate->error) {
	die("<strong>Error: </strong>" . $validate->error);
}

// 2. Get a list of friends IDs and followers IDs
$friends = simplexml2array(api("http://twitter.com/friends/ids.xml"));
if (isset($friends['id'])) {
	$friends = $friends['id'];
}
else {
	echo "Error getting friends!";
	exit;
}

// 3. Unfollow everyone!
if (is_array($friends) && count($friends) > 0) {
	foreach($friends as $f){
	    $xml = api("http://twitter.com/friendships/destroy/" . $f . ".xml", "DELETE");
	    if ($error = $xml->error) {
	        echo "<b>ERROR:</b> " . $f . "(" . $error. ")<br />";
	    }
		else {
			echo "Successfully unfollowed: " .  $f . "<br />";
	    }
		flush();
	}
}

// **************************************************
// Functions

// Put all SimpleXML Object values into array so we can parse them with PHP's array tools
function simplexml2array($xml) {
   if (get_class($xml) == 'SimpleXMLElement') {
       $attributes = $xml->attributes();
       foreach($attributes as $k=>$v) {
           if ($v) $a[$k] = (string) $v;
       }
       $x = $xml;
       $xml = get_object_vars($xml);
   }
   if (is_array($xml)) {
       if (count($xml) == 0) return (string) $x; // for CDATA
       foreach($xml as $key=>$value) {
           $r[$key] = simplexml2array($value);
       }
       if (isset($a)) $r['@'] = $a;    // Attributes
       return $r;
   }
   return (string) $xml;
}

// Calls Twitter API and returns SimpleXML Object
function api($url, $method="GET"){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($ch, CURLOPT_USERPWD, USERNAME.":".PASSWORD);
    $buffer = curl_exec($ch);
    curl_close($ch);
    return(simplexml_load_string($buffer));
}

Initial URL


Initial Description


Initial Title
unfollow all your followers on Twitter

Initial Tags
twitter

Initial Language
PHP