Estás en: Inicio » Blog»Código autofollow en PHP para Twitter

Código autofollow en PHP para Twitter


Twitter follow Ya sabemos que Twitter es la moda del microblogging, y si podemos interactuar intercambiando informaciones entre Twitter y nuestro blog y la forma de seguir a los que escriben sobre algunos términos específicos, podríamos hacerlo.

Si deseas seguir a todos aquellos que escriben en Twitter sobre “WordPress”, por ejemplo, solo tenemos que tener en cuenta el siguiente código:

Debemos cambiar los valores USUARIO, PASSWORD y TERMINOS FAVORITOS

<?php

$user = "USUARIO";
$pass = "PASSWORD";

$term = "TERMINOS FAVORITOS";

$userApiUrl = "http://twitter.com/statuses/friends.json";

$ch = curl_init($userApiUrl);
curl_setopt($ch, CURLOPT_USERPWD, $user.":".$pass);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$apiresponse = curl_exec($ch);
curl_close($ch);
$followed = array();

if ($apiresponse) {
	$json = json_decode($apiresponse);
	if ($json != null) {
		foreach ($json as $u) {
			$followed[] = $u->name;
		}
	}
}

$userApiUrl = "http://search.twitter.com/search.json?q=" . $term . "&rpp=100";
$ch = curl_init($userApiUrl);
curl_setopt($ch, CURLOPT_USERPWD, $user.":".$pass);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$apiresponse = curl_exec($ch);
curl_close($ch);

if ($apiresponse) {
	$results = json_decode($apiresponse);
	$count = 20;
	if ($results != null) {
		$resultsArr = $results->results;
		if (is_array($resultsArr)) {
			foreach ($resultsArr as $result) {
				$from_user = $result->from_user;
				if (!in_array($from_user,$followed)) {
					$ch = curl_init("http://twitter.com/friendships/create/" . $from_user . ".json");
					curl_setopt($ch, CURLOPT_USERPWD, $user.":".$pass);
					curl_setopt($ch, CURLOPT_POST, 1);
					curl_setopt($ch, CURLOPT_POSTFIELDS,"follow=true");
					curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
					$apiresponse = curl_exec($ch);

					if ($apiresponse) {
						$response = json_decode($apiresponse);
						if ($response != null) {
							if (property_exists($response,"following")) {
								if ($response->following === true) {
									echo "Ahora siguiendo " . $response->screen_name . "n";
								} else {
									echo "No puedes seguir " . $response->screen_name . "n";
								}
							} else {
								echo "Excedido el límite de usuarios a seguir." . $from_user . "n";
							}
						}
					}
					curl_close($ch);
				} else {
					echo "Siguiendo... " . $from_user . "n";
				}
			}
		}
	}
}
?>

El código ha sido realizado por Dave Stevens.


Share

© 2012 Todos los derechos reservados. Vera's Soul

Volver arriba