Skip to content

Instantly share code, notes, and snippets.

@stirtingale
Last active November 18, 2019 08:13
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save stirtingale/87e44b865343061edc9b09c14571fceb to your computer and use it in GitHub Desktop.
Save stirtingale/87e44b865343061edc9b09c14571fceb to your computer and use it in GitHub Desktop.
Simple PHP function to convert an Instagram ID into a URL.
// based on ggwarpig stackoverflow anwser to
// "Where do I find the Instagram media ID of a image"
// @ https://stackoverflow.com/a/37246231
function instagram_id_to_url($instagram_id){
$url_prefix = "https://www.instagram.com/p/";
if(!empty(strpos($instagram_id, '_'))){
$parts = explode('_', $instagram_id);
$instagram_id = $parts[0];
$userid = $parts[1];
}
$alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
while($instagram_id > 0){
$remainder = $instagram_id % 64;
$instagram_id = ($instagram_id-$remainder) / 64;
$url_suffix = $alphabet{$remainder} . $url_suffix;
};
return $url_prefix.$url_suffix;
}
// example
$insta_id = "XXX_XXX";
echo instagram_id_to_url($insta_id);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment