Skip to content

Instantly share code, notes, and snippets.

@woxly
Last active July 5, 2023 03:44
Show Gist options
  • Save woxly/a51c81b8d1de17dd9cf74a11ad2aef6c to your computer and use it in GitHub Desktop.
Save woxly/a51c81b8d1de17dd9cf74a11ad2aef6c to your computer and use it in GitHub Desktop.
Grab a Bitmoji of a Snapchat with Ease!

getBitmoji

Grab the Bitmoji of a Snapchat User with Ease

What would I use this for?

Well, I believe there's plenty of uses. It automatically updates to the current snapchat bitmoji selfie you're using on your profile. Download the image and use it as a profile picture else where, maybe.

How do I use it?

Place getBitmoji.php either on a local webserver, or upload it to the web host.

Usage

The script uses the $_GET param to grab the snapchat username from the URL posted so for example:

domain.tld[or]localhost/getBitmoji.php?snapchat={{username}}

Bitmoji Dimensions: 205x205

Note: I created all of this in like 15-mins on 0.35923 hours of sleep; however, I do plan on porting this to JavaScript due to the popularity of the language these days.

<?php
if(isset($_GET['snapchat'])){
// Set as a variable for nicer use
$snapchat = $_GET['snapchat'];
// Grab contents from Snapchat App API
$html = file_get_contents('https://app.snapchat.com/web/deeplink/snapcode?username='.$snapchat.'&type=SVG&bitmoji=enable');
// Start a new DOMDocument for loading into the script
$dom = new DOMDocument;
// Load into the HTML with Suppression
@$dom->loadHTML($html);
// Get the <image> element from the URL
$getImage = $dom->getElementsByTagName('image');
// Check if User has bitmoji
if($getImage->length==0){
die('User does not have Bitmoji.');
}
// Loop through the Images & grab the 'xlink:href' attribute
foreach($getImage as $img){
$bitmojiData = $img->getAttribute('xlink:href');
}
// Replace the beginning to convert to Base64
$base64 = str_replace('data:image/png;base64,', '', $bitmojiData);
// Decode the Base64 so we can flop it into a PNG
$base64 = base64_decode($base64);
// Create an image from $base64
$bitmoji = imagecreatefromstring($base64);
if ($bitmoji !== false) {
// Change the Script Content-Type to image/png so it can be used throughout the internet
header('Content-Type: image/png');
// Make sure to keep the Alpha set to true, unless you want a black background
imagesavealpha($bitmoji, true);
// Create the PNG
imagepng($bitmoji);
// Free up some memory
imagedestroy($bitmoji);
}
else {
echo 'An error occurred.';
}
} else {
echo 'Snapchat username must be set!';
}
?>
@EnzoDeg40
Copy link

I get the error "User does not have Bitmoji." with any username.
When I make a echo $dom->saveHTML(); after @$dom->loadHTML($html); here is the result :

‹ ÿt»W�ìÜ’%ö>¿âÃ7 �ê¦MšVßdÒÛ¤woôÞ“IóëµÏE÷ÌH�pª*++¹InFÄŠµVüû�¿†þ¯_±nÍ4þãoô_‘¿ÿ*ÆlÊ›±úÇßž+üý÷_Ûž
...

@woxly
Copy link
Author

woxly commented Apr 1, 2023

I get the error "User does not have Bitmoji." with any username. When I make a echo $dom->saveHTML(); after @$dom->loadHTML($html); here is the result :

‹ ÿt»W�ìÜ’%ö>¿âÃ7 �ê¦MšVßdÒÛ¤woôÞ“IóëµÏE÷ÌH�pª*++¹InFÄŠµVüû�¿†þ¯_±nÍ4þãoô_‘¿ÿ*ÆlÊ›±úÇßž+üý÷_Ûž
...

@EnzoDeg40 I wrote this over 3 years ago and haven't touched php in awhile. From my understanding, this error can be 1 of 2 reasons.

    1. Your host provider/machine doesn't have certain PHP Extensions Enabled - You can check this by using the get_loaded_extensions() function.
    1. You don't have access to the deeplink snapchat API;
https://app.snapchat.com/web/deeplink/snapcode?username={{ Your Snapchat Username Here }}&type=SVG&bitmoji=enable

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