Last active
December 12, 2019 21:20
-
-
Save 8lane/8197213 to your computer and use it in GitHub Desktop.
Get number of twitter followers API 1.1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function getTwitterFollowers($screenName = 'wpbeginner') | |
{ | |
// some variables | |
$consumerKey = 'YIQPxqfqQto5yaskourlA'; | |
$consumerSecret = 'OH3xiYM4oN3mjGK3as9m37zkKeyiHgKhBIgiNhoM'; | |
$token = get_option('cfTwitterToken'); | |
// get follower count from cache | |
$numberOfFollowers = get_transient('cfTwitterFollowers'); | |
// cache version does not exist or expired | |
if (false === $numberOfFollowers) { | |
// getting new auth bearer only if we don't have one | |
if(!$token) { | |
// preparing credentials | |
$credentials = $consumerKey . ':' . $consumerSecret; | |
$toSend = base64_encode($credentials); | |
// http post arguments | |
$args = array( | |
'method' => 'POST', | |
'httpversion' => '1.1', | |
'blocking' => true, | |
'headers' => array( | |
'Authorization' => 'Basic ' . $toSend, | |
'Content-Type' => 'application/x-www-form-urlencoded;charset=UTF-8' | |
), | |
'body' => array( 'grant_type' => 'client_credentials' ) | |
); | |
add_filter('https_ssl_verify', '__return_false'); | |
$response = wp_remote_post('https://api.twitter.com/oauth2/token', $args); | |
$keys = json_decode(wp_remote_retrieve_body($response)); | |
if($keys) { | |
// saving token to wp_options table | |
update_option('cfTwitterToken', $keys->access_token); | |
$token = $keys->access_token; | |
} | |
} | |
// we have bearer token wether we obtained it from API or from options | |
$args = array( | |
'httpversion' => '1.1', | |
'blocking' => true, | |
'headers' => array( | |
'Authorization' => "Bearer $token" | |
) | |
); | |
add_filter('https_ssl_verify', '__return_false'); | |
$api_url = "https://api.twitter.com/1.1/users/show.json?screen_name=$screenName"; | |
$response = wp_remote_get($api_url, $args); | |
if (!is_wp_error($response)) { | |
$followers = json_decode(wp_remote_retrieve_body($response)); | |
$numberOfFollowers = $followers->followers_count; | |
} else { | |
// get old value and break | |
$numberOfFollowers = get_option('cfNumberOfFollowers'); | |
// uncomment below to debug | |
//die($response->get_error_message()); | |
} | |
// cache for an hour | |
set_transient('cfTwitterFollowers', $numberOfFollowers, 1*60*60); | |
update_option('cfNumberOfFollowers', $numberOfFollowers); | |
} | |
return $numberOfFollowers; | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
test |
How is this different from tweet.user.followers_count? I am looking for code that gets the number of followers at the time of a tweet, rather than the current number of followers as followers_count does.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Source: http://www.wpbeginner.com/wp-tutorials/displaying-the-total-number-of-twitter-followers-as-text-on-wordpress/