Skip to content

Instantly share code, notes, and snippets.

@jptksc
Created August 9, 2013 15:07
Show Gist options
  • Save jptksc/6194391 to your computer and use it in GitHub Desktop.
Save jptksc/6194391 to your computer and use it in GitHub Desktop.
Get and cache any Twitter profile image without the Twitter API.
function get_twitter_profile_img($username, $size = '') {
$api_call = 'https://twitter.com/users/'.$username.'.json';
$results = json_decode(file_get_contents($api_call));
$image_url = str_replace('_normal', $size, $results->profile_image_url);
// Replace with your cache directory.
$image_path = './cache/';
// Get the name of the file.
$exploded_image_url = explode("/",$image_url);
$image_filename = end($exploded_image_url);
$exploded_image_filename = explode(".",$image_filename);
$extension = end($exploded_image_filename);
// Make sure its an image.
if($extension=="gif"||$extension=="jpg"||$extension=="jpeg"||$extension=="png"){
// Get the remote image.
$image_to_fetch = file_get_contents($image_url);
// Save it.
$local_image_file = fopen($image_path.$image_filename, 'w+');
chmod($image_path.$image_filename,0755);
fwrite($local_image_file, $image_to_fetch);
fclose($local_image_file);
}
return $image_path.$image_filename;
}
@chriswallace
Copy link

Nice snippet. I'm not sure you're understanding what an "API" actually is though. If you're accessing twitter's data via a json data feed, you're accessing part of their API. What you probably meant to say was "without requiring authentication."

@jptksc
Copy link
Author

jptksc commented Aug 13, 2013

That is indeed what I meant.

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