twitteroauth icon indicating copy to clipboard operation
twitteroauth copied to clipboard

Weird follower count bug

Open KJThaDon opened this issue 12 years ago • 17 comments

Not sure why this is happening..

I am using the code below to display a twitter follower count. What's happening is it displays fine here on the script im working on > http://ngx2gaming.com/kjthadon_social/

Although once I include the file above onto the front page sidebar of the site, twitter is the only one that wont display .. As you can see here http://ngx2gaming.com/

Anyone have an idea why it wont display on my front page, but will when I access the file directly?

Thanks

<?php
require_once ('twitteroauth.php');
require_once ('OAuth.php');

$twitter_id = 'ngx2gaming';
$twitter_access_token = 'xxx';
$twitter_access_token_secret = 'xxx';
$twitter_consumer_key = 'xxx';
$twitter_consumer_secret = 'xxx';

$twitter_api = new TwitterOAuth( $twitter_consumer_key , $twitter_consumer_secret , $twitter_access_token , $twitter_access_token_secret );
$twitter_api_link = $twitter_api->get( 'users/lookup' , array ( 'screen_name' => ( $twitter_id ) ) );
$twitter_count = $twitter_api_link[0]->followers_count;
echo $twitter_count;
?>

KJThaDon avatar Apr 21 '14 00:04 KJThaDon

I don't see anything wrong with the code you have posted. Are you sure the number is actually getting rendered in the template for the full page?

abraham avatar Apr 21 '14 21:04 abraham

@abraham thank you for the response!

It definitely is..I have it included in a php file along with the other social networks on that page..

That's what I mean, It's really weird..

KJThaDon avatar Apr 21 '14 23:04 KJThaDon

Maybe depending on which file you access the twitter config file is getting imported or not. You should check your server logs.

abraham avatar Apr 22 '14 01:04 abraham

@abraham

what do you mean by which file you access? I have just done a bunch of different debugs, and it's driving me nuts lol. Even tried changing all the variable names..

I even removed the twitter iframe feeds I had in the footer to see if that somehow interfered with it.

I have all the functions in one php file including the twitter one I posted above. Really not sure what to try anymore.

One thing I did find out is that it's only the front page. Any other page that has my sidebar included show it..like this

http://ngx2gaming.com/beachhead-studios-announces-clan-wars-in-dubai-and-vancouver/

Thanks for your help!

KJThaDon avatar Apr 22 '14 01:04 KJThaDon

What is the value of $twitter_api->http_code after the $twitter_api->get line?

abraham avatar Apr 22 '14 01:04 abraham

@abraham The output value? It's 200

KJThaDon avatar Apr 22 '14 01:04 KJThaDon

So that means Twitter is not returning an error. What is the value of $twitter_api_link? Does it contain a full user object with a followers_count in it?

abraham avatar Apr 22 '14 02:04 abraham

Hmm, I checked the last output on the direct link /kjthadon_social which returned 200

Although I should of been checking it on the front page, which returns NULL when i do a var_dump..

Still working on the direct page, that you can view the dump on the working page > http://ngx2gaming.com/kjthadon_social/

I am not understanding it right now lol. Seems like my domain is blocked or something :camel:

KJThaDon avatar Apr 22 '14 02:04 KJThaDon

It can't be your domain because it works on all the pages but one.

So even with the NULL you get a 200 for http_code before it?

abraham avatar Apr 22 '14 02:04 abraham

@abraham I know, just a crazy thought maybe its only the domain without any subpages idk..

No the front page is just NULL. when you asked me before, I mistakenly checked it on the /kjthadon_social which was always working, so of course that would be 200.

The front page is returning NULL. Which is really weird since it's the same file! I left it with the oputput displaying on both pages, if you wanted to see.

I don't get it. I even tried making a new app.

KJThaDon avatar Apr 22 '14 02:04 KJThaDon

Even works on a whole separate page with the same file included... http://ngx2gaming.com/social-testing/

KJThaDon avatar Apr 22 '14 03:04 KJThaDon

Something about your PHP is different on the front page than on the other pages. I would check to make sure all the PHP vars you are passing TwitterOAuth and the get request are exactly what they should be and the same for all pages.

abraham avatar Apr 22 '14 03:04 abraham

@abraham

There can't be a difference? It is simply included

That's in my sidebar. How could it be different?

KJThaDon avatar Apr 22 '14 03:04 KJThaDon

I have no insight into your code as I've never see it. But consistently works on all pages but one. Logically there is something different about that one page.

abraham avatar Apr 22 '14 03:04 abraham

Hmm I added the code to the last post, but it seems it removes itself.

this is what I posted. This is how my front page sidebar is linked to it.

http://pastebin.com/eRETKzgn

KJThaDon avatar Apr 22 '14 03:04 KJThaDon

I see you do an include on the main page, do you have other includes on the kjthadon_social/index.php script? If so, it's possible the references are not OK as the includes are no absolute paths but relatives.

Do you own this server? Can you verify the Apache/NGINX error/warning log? If not, can you enable/increase the PHP Warning output level?

I'm quite sure you'll have some file_not_found errors when you include it on the main website. (eg Twitter classes not found)

Beside this, it's maybe not that nice to do a call to Twitter on each page-view you got. Maybe you should think of a caching system your side? Eg call Twitter every 5 minutes to get the new followers count, save this number in your database, or a file, ... and use this on every file. It will help speeding up your website. Unless you really want to have the number realtime ofc.

jelledelaender avatar Apr 22 '14 06:04 jelledelaender

Hello. i'm trying to use this code above. I have a fatal error on this line $twitter_count = $twitter_api_link[0]->followers_count;.

This it the error Fatal error: Cannot use object of type stdClass as array

Please how can i solve it.

Best regards.

tg-alexis avatar Feb 21 '15 17:02 tg-alexis