I am trying to get my twitter follower count to show on my website. I have set up the twitter API to receive the follower count, and that has been tested using echo and that is working perfectly.
The problem is I can’t work out how to pass that value into the html.
This is the code that gets the twitter count:
<?php
/*
* Requires the "Twitter API" wrapper by James Mallison
* to be found at https://github.com/J7mbo/twitter-api-php
*
* The way how to get a follower count was posted by Amal Murali
* on http://stackoverflow.com/questions/17409227/follower-count-number-in-twitter
*/
require_once('twitterapi/TwitterAPIExchange.php'); // adjust server path accordingly
// GET YOUR TOKENS AND KEYS at https://dev.twitter.com/apps/
$settings = array(
'oauth_access_token' => "SECRET", // enter your data here
'oauth_access_token_secret' => "SECRET", // enter your data here
'consumer_key' => "SECRET", // enter your data here
'consumer_secret' => "SECRET" // enter your data here
);
$ta_url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
$getfield = '?screen_name=SECRET'; // enter your twitter name without the "@" here
$requestMethod = 'GET';
$twitter = new TwitterAPIExchange($settings);
$follow_count=$twitter->setGetfield($getfield)
->buildOauth($ta_url, $requestMethod)
->performRequest();
$data = json_decode($follow_count, true);
$followers_count = $data[0]['user']['followers_count'];
?>
And this is the code to pass the $followers_count variable into the html.
It’s a span tag with the id “twit-follow-count”.
<script type="text/javascript">
$( document ).ready(function() {
$('#twit-follow-count').html($followers_count);
});
</script>
tried doing it for my website https://sthelensremovals.com/
Any help would be greatly appreciated!
This is more of a PHP / javascript / web development question - you might get an answer faster on stackoverflow.com instead.
For the twitter specific part: I’d make sure the code for getting the followers count does not run every time the page is requested - you’ll want to cache this value in a database or a temporary file or something like that and update it periodically, otherwise it will break or return incorrect results after the rate limit is hit (depends how the library handles errors)
Also, users/show GET users/show | Docs | Twitter Developer Platform is probably a better choice for this - it will return just the user, a much smaller and more efficient request than user_timeline for this purpose.
system
Closed
#3
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.