Let’s take a look how php code interacts with twitter.
1.) How to get number of twitter followers.
- fnction get_followers($twitter_id){
- $xml=file_get_contents('http://twitter.com/users/show.xml?screen_name='.$twitter_id);
- if (preg_match('/followers_count>(.*)</',$xml,$match)!=0) {
- $tw['count'] = $match[1];
- }
- return $tw['count'];
- }
Now call this function with this code
- $followers = get_followers(‘vimalm4u’);
- echo “You have”.$followers.”followers!”;
2.) Get number of retweets for a specific page.
Function will get the number of retweets of the url passed as a parameter
- function rtweetCount($url) {
- $content = file_get_contents("http://api.tweetmeme.com/url_info?url=".$url);
- $element = new SimpleXmlElement($content);
- $retweets = $element->story->url_count;
- if($retweets){
- return $retweets;
- } else {
- return 0;
- }
- }
- echo rtweetCount('http://www.coffeewithcode.com');
3) Shorten urls for Twitter
As we know, we can’t tweets messages which are longer than 140 characters. To avoid this problem, you have to use an url shortener.
- function getTinyUrl($url) {
- return file_get_contents("http://tinyurl.com/api-create.php?url=".$url);
- }