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
Comment #1
InTheLyonsDen commentedThanks 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.
Comment #2
drupalninja99 commentedYa 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.
Comment #3
araujo.guntin commentedHi...
Could I put these code in a block?
Thanks
Comment #4
Mankot commentedHey 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:
But it doesn't work at all. Any suggestions? I'm really stuck with this for a while now.
Thanks in advance
Comment #5
Mankot commentedWoow, 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:
Thx everyone :)
Comment #6
steinmb commentedComment #7
paulhudson commentedI'm having a rotten time with this.
I've extended the Twitter class in my custom module:
If I load the extension with:
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:
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.
Comment #8
paulhudson commentedIt 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:
Comment #9
Jaesin commentedGood suggestions but with no patch against the current branch. There is nothing to do here.
Comment #10
aufumy commentedFor people that want to add a 'Follow' link say in the views header text, use:
If you want to add it to a page that does not have a twitter view loaded on it, use: