I ended up creating a custom module to piggyback the twitter module, in doing so I added 2 functions which you could potentially roll up into the twitter module if you so choose, here they are:

twitter_follow_user - follow a user account
twitter_search - perform a search (this probably needs some more parameters but it works)

I think it'd be good to grow the twitter API so that this module can be used by other twitter modules

/**
 * Follow user account
 * 
 * @param $screen_name
 * @param $password
 * @param $follow_screen_name
 * 
 * @return
 *   The full results of the Drupal HTTP request, including the HTTP response
 *   code returned by Twitter.com.
 */
function twitter_follow_user($screen_name, $password, $follow_screen_name) {
  $url = "http://" . variable_get('twitter_api_url', 'twitter.com') . "/friendships/create.xml?screen_name=$follow_screen_name";
  
  $headers = array('Authorization' => 'Basic '. base64_encode($screen_name .':'. $password),
                   'Content-type' => 'application/x-www-form-urlencoded');
  $results = drupal_http_request($url, $headers, 'POST');
  if (_twitter_request_failure($results)) {
    return array();
  }
  
  return _twitter_convert_xml_to_array($results->data);
}

/**
 * Follow user account
 * 
 * @param $screen_name
 * @param $password
 * @param $follow_screen_name
 * 
 * @return
 *   The full SimpleXML converted results
 */
function twitter_search($keywords) {
  $url = "http://search." . variable_get('twitter_api_url', 'twitter.com') . "/search.atom?q=".urlencode($keywords)."&show_user=true&rpp=100";
  
  $results = drupal_http_request($url, array(), 'GET');
  if (_twitter_request_failure($results)) {
    return array();
  }

  $xml = new SimpleXMLElement($results->data);

  return $xml->entry;
}

Comments

InTheLyonsDen’s picture

Thanks for the code! Do I simply add this to the bottom of the "twitter.module" file? I am in desperate need of populating the "twitter" table with search results along with the normal data from the Twitter module with the "twitter_id" and "screen_name" fields referencing the tweet author's name. Am I on the right track using your search code?

Thanks in advance for your help. I am a great implementer/designer with Drupal but a hack when it comes to code.

drupalninja99’s picture

Ya you could hack the module, but I would recommend adding it in a module like twitter_extra or something like that so you can still have upgrades. That's actually exactly what I did.

araujo.guntin’s picture

Hi...

Could I put these code in a block?

Thanks

Mankot’s picture

Version: 6.x-2.6 » 6.x-3.0-beta2

Hey everyone,

I need a "follow function" for the newest beta version of the twitter module (using oauth). I tried to extend the twitter.lib.php using the following code:

  public function create_friendship($id, $params = array()) {
    if (is_numeric($id)) {
      $params['user_id'] = $id;
    }
    else {
      $params['screen_name'] = $id;
    }    
    $params['follow'] = TRUE;
    $values = $this->call('friendships/create', $params, 'POST', TRUE); 
    
  return $values;
  }

But it doesn't work at all. Any suggestions? I'm really stuck with this for a while now.
Thanks in advance

Mankot’s picture

Woow, actually at second glance my code works like a charm. There must have been some error with further implementations of that function. Extending the code with the following function in a custom module works fine:

function twitter_firmentweets_friendship_create($screen_name, $account_name) {
  $account = twitter_account_load($account_name);  
  $twitter = twitter_connect($account);
  
  $friend = $twitter->create_friendship($screen_name);

	return $friend;
}

// Using the function
twitter_firmentweets_friendship_create('drupal');

Thx everyone :)

steinmb’s picture

Version: 6.x-3.0-beta2 » 6.x-3.x-dev
paulhudson’s picture

I'm having a rotten time with this.

I've extended the Twitter class in my custom module:

/**
 * Extend the twitter class in twitter.lib.php
 **/
class twitter_user_extend extends Twitter {
	
	public function __construct() {}

	public function followers_ids($id, $use_auth = TRUE) {
    $params = array();
    if ($id) {
      $params['user_id'] = $id;
    }
    else {
      return false;
    }

    $values = $this->call('followers/ids', $params, 'GET', $use_auth);
    return $values;

  }
	
	public function users_lookup($id, $use_auth = TRUE) {
    $params = array();
    if (is_string($id)) {
      $params['user_id'] = $id;
    }
    else {
      return false;
    }

    $values = $this->call('users/lookup', $params, 'GET', $use_auth);
    return $values;

  }
}

/**
 * Get tweets menitoning the user
 **/
function social_tracker_twitter_get_followers() {
	
	global $user;

	$twitter_accounts = twitter_get_user_accounts($user->uid);

	$statuses = array();
	foreach ($twitter_accounts as $twitter_account) {

	$account = twitter_account_load($twitter_account); 
		
		
		$twitter_user_custom = new twitter_user_extend;
		//$twitter_user_custom = twitter_connect($account);  // I think Auth GET requests require this but it doesn't work with my extended functions
		
		$results = $twitter_user_custom->followers_ids($twitter_account->id);
	
		$lookup = $twitter_user_custom->users_lookup($results->ids);
		
	}
	
	return $results;
}

If I load the extension with:

$twitter_custom = new twitter_user_extend;

$results = $twitter_custom->followers_ids($twitter_account->id);

My custom extended functions run ok but because I didn't use twitter_connect() the GET requests that require auth fail.

Trouble is, if I do use the twitter_connect() function:

$twitter= twitter_connect($account);

$results = $twitter->followers_ids($twitter_account->id);

followers_ids() isn't found...
PHP Fatal error: Call to undefined method Twitter::followers_ids()

Naturally I don't want to dump my extended functions into the actual Twitter class... however tempting ;-)

Bit embarrassing I can get this working! I'd love some help.

paulhudson’s picture

It was fairly obvious as it always tends to be!

I needed to extend the TwitterOAuth class... that's the one that has the users account stuff.

Here's an example for anyone else:


/**
 * Extend the twitter class in twitter.lib.php
 **/
class twitter_user_extend extends TwitterOAuth {
	
	# Do we really need to do this? Looks a bit clumsy...
	public function __construct($consumer_key, $consumer_secret, $oauth_token = NULL, $oauth_token_secret = NULL) {
		
		parent::__construct($consumer_key, $consumer_secret, $oauth_token = NULL, $oauth_token_secret = NULL);
	}
	
	# dev.twitter.com/docs/api/1/get/users/lookup
	public function followers_ids($id) {
		
    $params = array();
    if ($id) {
      $params['user_id'] = $id;
    }
    else {
      return false;
    }
		
		$url = $this->create_url('1/followers/ids'); // Must include the version number (1)

    $response = $this->auth_request($url, $params, 'GET'); // No need for @use_auth declaration as we're using OAuth
		
		return $response;

  }
	
	# dev.twitter.com/docs/api/1/get/users/lookup
	public function users_lookup($id) {
    $params = array();
    if (is_string($id)) {
      $params['user_id'] = $id;
    }
    else {
      return false;
    }
		
		$url = $this->create_url('1/users/lookup'); // Must include the version number (1)

    $response = $this->auth_request($url, $params, 'GET'); // No need for @use_auth declaration as we're using OAuth
		
		return $response;
  }
}

/**
 * Helper function to load Twitter classes
 * This is a copy of twitter_connect()
 **/
function social_tracker_twitter_connect($account) {
  module_load_include('php', 'oauth_common','lib/OAuth');
  $auth = $account->get_auth();
  if (_twitter_use_oauth() && $auth['oauth_token'] && $auth['oauth_token_secret']) {
    module_load_include('lib.php', 'oauth');
    return new twitter_user_extend(variable_get('twitter_consumer_key', ''), variable_get('twitter_consumer_secret', ''),
                            $auth['oauth_token'], $auth['oauth_token_secret']);
  }
  else {
    return new Twitter;
  }
}

/**
 * Get tweets menitoning the user
 **/
function social_tracker_twitter_get_followers() {
	
	global $user;
	
	$twitter_accounts = twitter_get_user_accounts($user->uid);

	$statuses = array();
	foreach ($twitter_accounts as $twitter_account) {
		
		// Load custom extention class
		$extended_twitter = social_tracker_twitter_connect($twitter_account);

		$lookup = $extended_twitter->followers_ids($twitter_account->id);
		
		$results = $twitter->users_lookup($results->ids);
		
		$return[] = $results;
	}
	
	return $return;
}
Jaesin’s picture

Status: Active » Closed (won't fix)

Good suggestions but with no patch against the current branch. There is nothing to do here.

aufumy’s picture

For people that want to add a 'Follow' link say in the views header text, use:

<span><a href="https://twitter.com/intent/user?screen_name=twitter_screen_name">Follow</a></span>

If you want to add it to a page that does not have a twitter view loaded on it, use:

    drupal_add_js('//platform.twitter.com/widgets.js', 'external');
    echo '<span><a href="https://twitter.com/intent/user?screen_name=' . $twitter_screen_name . '">Follow</a></span>';