[PHP] Displaying the amount of twitter followers you have
Well, I section of my latest website had to display the amount of twitter followers that I had. After searching around and finding stupidly long and unclean functions, I decided to create my own..
PHP Code:
<?php
function getTwitterFollowers($userid) {
$url = "http://twitter.com/users/show.xml?screen_name=$userid";
$xml = simplexml_load_file($url) or die ("Error fetching twitter XML");
echo $xml->followers_count . " followers.";
}
?>
Might come in useful for someone :) you can also modify it for other things such as displaying your latest tweet.
Usage
PHP Code:
getTwitterFollowers("USERNAME");
:)
Re: [PHP] Displaying the amount of twitter followers you have
Re: [PHP] Displaying the amount of twitter followers you have
so this is more of a release then asking for help, gotcha
Re: [PHP] Displaying the amount of twitter followers you have
Yeah, I posted in the wrong section but I've requested it to be moved - just waiting for the moderator to move it :)
Re: [PHP] Displaying the amount of twitter followers you have
Im gonna move it asap when i get home, this phone ui wont let me move threads :/
Posted via Mobile Device
Re: [PHP] Displaying the amount of twitter followers you have
Its not a bad idea, and I'm glad to see you attempt to handle what to do if you can't connect to Twitter, but die is NOT the answer.
Die is going to halt your whole script from running, and I personally wouldn't want to rely on Twitter that much. If you can't connect then return something else, or do something else there, but don't use a die statement.
Re: [PHP] Displaying the amount of twitter followers you have
and what I wouldn't use is simple_xml_load which I believe is slow.. or slower than this anyway
PHP Code:
function followersNum($id) {
$jsonArray = json_decode('http://api.twitter.com/1/users/show/'.$id.'.json?callback=?', true);
return $jsonArray['followers_count'];
}
or what I'd do because it's more fancy
Edit this Fiddle - jsFiddle - Online Editor for the Web (JavaScript, MooTools, jQuery, Prototype, YUI, Glow and Dojo, HTML, CSS)
Re: [PHP] Displaying the amount of twitter followers you have
Okay, well it's my first time ever fetching data from an XML file, so it was quite basic. What should I use instead of die() ?
@foxx the XML file is very unreliable. I'm using a similar script to load my latest tweet, whenever I retweet someone the XML always errors. And I'm going to take a look at the link now :)
Re: [PHP] Displaying the amount of twitter followers you have
Quote:
Originally Posted by
MattehCarter
Okay, well it's my first time ever fetching data from an XML file, so it was quite basic. What should I use instead of die() ?
@foxx the XML file is very unreliable. I'm using a similar script to load my latest tweet, whenever I retweet someone the XML always errors. And I'm going to take a look at the link now :)
If you're putting the follower count on the side of the screen for example, you don't want the script to echo an ugly error message such as "Can not parse XML file from Twitter", so why not just do:
PHP Code:
$xml = @simplexml_load_file( $url );
The @ will prevent an error message being displayed if the script failed to parse the XML file.
That's what I would do anyway.
Re: [PHP] Displaying the amount of twitter followers you have
Well you don't want the script to die because anything after that point won't exist during the execution of that script. Also Twitter is notorious for their down-time.. Dunno if they still are, but yeh I don't tweet anymore, every other 5 minutes their servers overload or what-have-you, and that stupid message pops up about how their rushing around fixing it... every five minutes.
And I thought we've established using the json from the twitter API makes a lot more sense than ripping through the xml... (which seems more likely to change than something provided by the API)
Just use foxx's example, and forget about XML for today.. ;)
Re: [PHP] Displaying the amount of twitter followers you have
Is there a way that I can also display my latest tweet via foxx's way?
Re: [PHP] Displaying the amount of twitter followers you have
easily, just wait couple of hours until I'm sober and I'll show you
Re: [PHP] Displaying the amount of twitter followers you have
Haha :') I've got it working now! Thanks anyway though man :)