Skip to content

Instantly share code, notes, and snippets.

@notasausage
Forked from banksy89/gist:3334259
Last active December 20, 2015 10:30
Show Gist options
  • Save notasausage/6115639 to your computer and use it in GitHub Desktop.
Save notasausage/6115639 to your computer and use it in GitHub Desktop.
Return the profile image (avatar) of a Twitter user using their RSS feed (no API call necessary).
<?php
function getTwitterAvatar( $username, $size = "normal" ) {
// mini = 24px, normal = 48px, bigger = 73px
$sizes = array( "mini", "normal", "bigger" );
// Check for size, default to normal
if( in_array( $size, $sizes ) ) {
$variant = $size;
} else {
$variant = "normal";
}
// Get the user's profile image from their RSS feed
$rawxml = simplexml_load_file( "http://twitter.com/users/{$username}.xml" );
$imageurl = $rawxml->profile_image_url;
// Return the image's url with the size requested
if( $variant == "normal" ) {
return $imageurl;
} else {
return str_replace( "normal", $variant, $imageurl );
}
}
?>
@notasausage
Copy link
Author

I should also note that the php.ini on your server must have allow_url_fopen = on in order for this to work, otherwise PHP will throw an error on the simplexml_load_file() function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment