Last active
December 11, 2017 05:16
-
-
Save hutt/4960630 to your computer and use it in GitHub Desktop.
Some functions using the Twitter API (v1.1) e.g. getting followers count, tweets, background image etc. Written in PHP; use it and have fun!
This file contains 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
function get_followers_count($username) { | |
$cache_file = CACHEDIR . 'twitter_followers_counter_' . md5 ( $username ); | |
if (is_file ( $cache_file ) == false) { | |
$cache_file_time = strtotime ( '1984-01-11 07:15' ); | |
} else { | |
$cache_file_time = filemtime ( $cache_file ); | |
} | |
$now = strtotime ( date ( 'Y-m-d H:i:s' ) ); | |
$api_call = $cache_file_time; | |
$difference = $now - $api_call; | |
$api_time_seconds = 1800; | |
if ($difference >= $api_time_seconds) { | |
$api_page = 'http://twitter.com/users/show/' . $username; | |
$xml = file_get_contents ( $api_page ); | |
$profile = new SimpleXMLElement ( $xml ); | |
$count = $profile->followers_count; | |
if (is_file ( $cache_file ) == true) { | |
unlink ( $cache_file ); | |
} | |
touch ( $cache_file ); | |
file_put_contents ( $cache_file, strval ( $count ) ); | |
return strval ( $count ); | |
} else { | |
$count = file_get_contents ( $cache_file ); | |
return strval ( $count ); | |
} | |
} | |
function get_followings_count | |
$cache_file = CACHEDIR . 'twitter_followings_counter_' . md5 ( $username ); | |
if (is_file ( $cache_file ) == false) { | |
$cache_file_time = strtotime ( '1984-01-11 07:15' ); | |
} else { | |
$cache_file_time = filemtime ( $cache_file ); | |
} | |
$now = strtotime ( date ( 'Y-m-d H:i:s' ) ); | |
$api_call = $cache_file_time; | |
$difference = $now - $api_call; | |
$api_time_seconds = 1800; | |
if ($difference >= $api_time_seconds) { | |
$api_page = 'http://twitter.com/users/show/' . $username; | |
$xml = file_get_contents ( $api_page ); | |
$profile = new SimpleXMLElement ( $xml ); | |
$count = $profile->friends_count; | |
if (is_file ( $cache_file ) == true) { | |
unlink ( $cache_file ); | |
} | |
touch ( $cache_file ); | |
file_put_contents ( $cache_file, strval ( $count ) ); | |
return strval ( $count ); | |
} else { | |
$count = file_get_contents ( $cache_file ); | |
return strval ( $count ); | |
} | |
} | |
function get_tweets_count{ | |
$cache_file = CACHEDIR . 'twitter_tweets_counter_' . md5 ( $username ); | |
if (is_file ( $cache_file ) == false) { | |
$cache_file_time = strtotime ( '1984-01-11 07:15' ); | |
} else { | |
$cache_file_time = filemtime ( $cache_file ); | |
} | |
$now = strtotime ( date ( 'Y-m-d H:i:s' ) ); | |
$api_call = $cache_file_time; | |
$difference = $now - $api_call; | |
$api_time_seconds = 1800; | |
if ($difference >= $api_time_seconds) { | |
$api_page = 'http://twitter.com/users/show/' . $username; | |
$xml = file_get_contents ( $api_page ); | |
$profile = new SimpleXMLElement ( $xml ); | |
$count = $profile->statuses_count; | |
if (is_file ( $cache_file ) == true) { | |
unlink ( $cache_file ); | |
} | |
touch ( $cache_file ); | |
file_put_contents ( $cache_file, strval ( $count ) ); | |
return number_format(strval($count), 0, ',', '.'); | |
} else { | |
$count = file_get_contents ( $cache_file ); | |
return number_format(strval($count), 0, ',', '.'); | |
} | |
} | |
function get_avatar_url($username) { | |
$cache_file = CACHEDIR . 'twitter_get_avatar_' . md5 ( $username ); | |
if (is_file ( $cache_file ) == false) { | |
$cache_file_time = strtotime ( '1984-01-11 07:15' ); | |
} else { | |
$cache_file_time = filemtime ( $cache_file ); | |
} | |
$now = strtotime ( date ( 'Y-m-d H:i:s' ) ); | |
$api_call = $cache_file_time; | |
$difference = $now - $api_call; | |
$api_time_seconds = 1800; | |
if ($difference >= $api_time_seconds) { | |
$api_page = 'http://twitter.com/users/show/' . $username; | |
$xml = file_get_contents ( $api_page ); | |
$profile = new SimpleXMLElement ( $xml ); | |
$count = $profile->profile_image_url; | |
if (is_file ( $cache_file ) == true) { | |
unlink ( $cache_file ); | |
} | |
touch ( $cache_file ); | |
file_put_contents ( $cache_file, strval ( $count ) ); | |
return strval ( $count ); | |
} else { | |
$count = file_get_contents ( $cache_file ); | |
return strval ( $count ); | |
} | |
} | |
function get_background_url($username) { | |
$cache_file = CACHEDIR . 'twitter_get_background_' . md5 ( $username ); | |
if (is_file ( $cache_file ) == false) { | |
$cache_file_time = strtotime ( '1984-01-11 07:15' ); | |
} else { | |
$cache_file_time = filemtime ( $cache_file ); | |
} | |
$now = strtotime ( date ( 'Y-m-d H:i:s' ) ); | |
$api_call = $cache_file_time; | |
$difference = $now - $api_call; | |
$api_time_seconds = 1800; | |
if ($difference >= $api_time_seconds) { | |
$api_page = 'http://twitter.com/users/show/' . $username; | |
$xml = file_get_contents ( $api_page ); | |
$profile = new SimpleXMLElement ( $xml ); | |
$count = $profile->profile_background_image_url; | |
if (is_file ( $cache_file ) == true) { | |
unlink ( $cache_file ); | |
} | |
touch ( $cache_file ); | |
file_put_contents ( $cache_file, strval ( $count ) ); | |
return strval ( $count ); | |
} else { | |
$count = file_get_contents ( $cache_file ); | |
return strval ( $count ); | |
} | |
} | |
function get_name($username) { | |
$cache_file = CACHEDIR . 'twitter_get_name' . md5 ( $username ); | |
if (is_file ( $cache_file ) == false) { | |
$cache_file_time = strtotime ( '1984-01-11 07:15' ); | |
} else { | |
$cache_file_time = filemtime ( $cache_file ); | |
} | |
$now = strtotime ( date ( 'Y-m-d H:i:s' ) ); | |
$api_call = $cache_file_time; | |
$difference = $now - $api_call; | |
$api_time_seconds = 1800; | |
if ($difference >= $api_time_seconds) { | |
$api_page = 'http://twitter.com/users/show/' . $username; | |
$xml = file_get_contents ( $api_page ); | |
$profile = new SimpleXMLElement ( $xml ); | |
$count = $profile->name; | |
if (is_file ( $cache_file ) == true) { | |
unlink ( $cache_file ); | |
} | |
touch ( $cache_file ); | |
file_put_contents ( $cache_file, strval ( $count ) ); | |
return strval ( $count ); | |
} else { | |
$count = file_get_contents ( $cache_file ); | |
return strval ( $count ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment