views:

44

answers:

4

Hello,

This is the code I am using, but it does return blank in most cases.

<?php
$xml=file_get_contents('http://twitter.com/users/show.xml?screen_name=MyTwitterUsernameBla');
if (preg_match('/followers_count>(.*)</',$xml,$match)!=0) {
$count = $match[1];
}
echo $count;
?>

I want to show Twitter followers count in simple way, no plugins..it's a website, and not a blog.

Thank you very much :)

A: 

When it returns blank, you could echo the $xml variable to help debugging.

For example, twitter may be returning an error message instead of the contents (perhaps they feel you are requesting the file too often ?).

I see nothing wrong with the code you posted in the sense that it works fine, so it's bound to come from something else.

wildpeaks
A: 

It's much better (more reliable and easier to maintain) to use an XML parser than to try to fetch bits with regular expressions.

Simply:

$xml = new SimpleXMLElement('http://twitter.com/users/show.xml?screen_name=username', null, true);

echo $xml->followers_count;
David Caunt
This code works on localhost, but not on my public host. What could be disabled on the host and denies it from working please?
Ahmad Fouad
Probably allow_url_fopen - http://uk.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen - try enabling all errors (error_reporting(E_ALL)) to get an error message, or you're going to be blindly debugging
David Caunt
I did that. No errors are shown, just blank.. :(
Ahmad Fouad
I just added echo ini_get('allow_url_fopen') it returned 1. So thats enabled.. I guess
Ahmad Fouad
Is display_errors set to On? Are any errors shown for anything?
David Caunt
I set display errors on. Nothing shown, just blank .. I tried to know if allow_url_fopen and curl are active, yes both active. It just won't get any data on the webserver. on localhost yes . thats driving me crazy
Ahmad Fouad
+1  A: 

Think your reg. exp. is too greedy.. believe it will match everything up until the last '< /'

try something like a non-greedy match:

/followers_count>(.*?)</

or match everything except '<' :

/followers_count>([^<]*)</

Parsing the XML might be easier, and if you want to extract a lot from the xml, more "correct". But seems like overkill in your case.

Øyvind Skaar
I tried the XML method. It does not parse anything either on webserver. On localhost everything is fine.. This is weird, I checked if xml parser is loaded and yes it is. What could be that
Ahmad Fouad
See my comment on my post. As both methods work locally, I'm guessing that allow_url_fopen on the remote host is disabled. You need to turn error reporting on to debug.
David Caunt
+2  A: 

If it works on your localhost but not on your public host, then chances are that Twitter is either blocking or throttling your public server's IP. Many shared hosts get blocked/throttled because there are multiple sites banging away at the Twitter API without authentication. Twitter views them as one app and throttles the IP as a whole.

To get around this, you're going to need to authenticate using oAuth. It's a pain in the ass, but it's a necessary step. Here are some resources that you might find to be handy:

Twitter offers you some credentials in the "Your Apps" section of the Twitter Developer website. Once you've registered your site as an app, click on My Access Token. That will give you a key/token that you can use to successfully authenticate.


Another thought is that Twitter is simply throttling your app. If this is the case, it's because you've got too much traffic and you're not caching the API results. Cache the results of the API for, say, an hour before fetching a new copy. Twitter will stop singling you out if you do this.

if(filemtime("cache.xml") < (int)$_SERVER["REQUEST_TIME"] - 3600) {
    $data = file_get_contents("http://twitter.com/users/show.xml?screen_name=username");
    file_put_contents("cache.xml", $data);
} else
    $data = file_get_contents("cache.xml");
mattbasta
Unbelieveably helpful! The cache thing did the trick..i searched the web everything for simple solution like that.. :)
Ahmad Fouad