doppelme logo

Example Integration Code

Below are 4 example PHP files that illustrate how to integrate DoppelMe avatars within your website using the SOAP API. These are given for example only and no warranty is provided. These example are given in PHP - but the same prinicples can be applied to code in other languages/frameworks.

doppelme.php | profile_avatar.php | profile_avatar_create.php | profile_avatar_updated.php

Example page illustrating options for creating a new avatar for your user

For users who do not yet have an avatar set up for your site, you can provide a way to create a new avatar account for them. Note that you could just automatically create an avatar account for all your users using the same principles.
<?php include("doppelme.php"); ?>


<?php
    
    session_start();
    
    //for testing we set session variable here    
    $_SESSION['UserID'] = "1";


    //your user's id (i.e. the id that your site uses to identify your user)
    if( isset($_SESSION['UserID']) ) {
        $USER_ID = $_SESSION['UserID'];
    } else {
        //user hasnt logged in or session expired?
        header( 'Location: http://[www.yoursite.com]/please_login.php' ); 
    }
            
                            
    //passed through from form
    if ( isset($_REQUEST["create_new"] ) ) {
        $CreateNew    = $_REQUEST["create_new"];
    } else {
        $CreateNew    = "";
    }
    if ( isset($_REQUEST["avatar_name"] ) ) {
        $AvatarName    = $_REQUEST["avatar_name"];
    } else {
        $AvatarName    = "";
    }    
    if ( isset($_REQUEST["doppelmekey"] ) ) {
        $DoppelMeKey = $_REQUEST["doppelmekey"];
    } else {
        $DoppelMeKey = "";
    }    
    if ( isset($_REQUEST["username"] ) ) {
        $Username    = $_REQUEST["username"];
    } else {
        $Username     = "";
    }
    if ( isset($_REQUEST["shadowkey"] ) ) {
        $ShadowKey    = $_REQUEST["shadowkey"];
    } else {
        $ShadowKey    = "";
    }            


    
    $sSELECT = "";    
    //should clean input
    
        //-------------------------------------------------------------
    // User has requested to create a new avatar
    //-------------------------------------------------------------
    if ($CreateNew == "Y" and $AvatarName != "") {

        
        //this example allows your user to choose their own label (doppelme_name) for their new avatar
        //if you are only going to allow them to use one avatar on your site, you could
        //just choose a label on their behalf.

        //creates a new account and returns us a doppelme key            
        $sURL        = "CreateDoppelMeAccount";    
        $sDATA        = array("partner_id"     => $DM_PARTNER_ID ,
                    "partner_key"     => $DM_PARTNER_KEY ,
                    "doppelme_name" => urlencode($AvatarName),
                    "partner_user_id" => $USER_ID ,
                    "referral_id"     => "0",
                    "callback_url"     => "a",
                    "is_test"     => false
                    );                
        
        $sReturnXML     = PartnerServiceCall($sURL, array($sDATA) );
        if ( ! $sReturnXML ) {        
               //problem?   
        } else {            
            $xmlDoc         = simplexml_load_string( $sReturnXML->CreateDoppelMeAccountResult );
            $sDOPPELME_KEY        = $xmlDoc->DoppelMeKey;
            $sVALIDATION_KEY    = $xmlDoc->ValidationKey;
            $sSTATUS        = $xmlDoc->StatusInfo;
            
            if (  $sDOPPELME_KEY == "" or $sDOPPELME_KEY == "N/A") {
                echo "There appears to have been a problem.";
            } else {
        
                    //you should update you own database to record this users key so that you can use it 
                    //around the rest of your site (sql code is given as example only)
                
                /*
                $sSQL = "UPDATE [YourUserTable] SET DoppelMeKey='" . sDOPPELME_KEY . "' WHERE UserID=" & $USER_ID                
                */
                
                //since were just testing, we store temporarily in session
                $_SESSION['DoppelMeKey'] = (String)$sDOPPELME_KEY;
                header( 'Location: profile_avatar.php' );                 
            }                    
        }    
    
    
    //-------------------------------------------------------------
    // User has selected one of their avatar keys to use on your site
    //-------------------------------------------------------------    
    } elseif ( $DoppelMeKey != "" ) {
        
        
        //always sanitize user input. DM keys should only ever consist of alpha-numeric values
        $DoppelMeKey  = CleanTaintedInput($DoppelMeKey);
        
        //we're saving it in session for testing but you store a permanent copy in your database for this user
        $_SESSION["DoppelMeKey"] = (String)$DoppelMeKey; 
        
        /*
        $sSQL = "UPDATE [YourUserTable] SET DoppelMeKey='" . $DoppelMeKey  . "' WHERE UserID=" . $USER_ID;
        oDB.Execute $sSQL;
        */

                
        header( 'Location: profile_avatar.php' );     

        //-------------------------------------------------------------
    // User already has a DoppelMe avatar - so retrieve the list of avatars
    // that they have and let them choose which one to associate with your site.
    //-------------------------------------------------------------                
    } elseif ($Username != "") {
            
        $sURL        = "AssignPartnerUserID";            
        $sDATA        = array("partner_id"         => $DM_PARTNER_ID, 
                    "partner_key"         => $DM_PARTNER_KEY,
                    "partner_user_id"     => $USER_ID, 
                    "doppelme_username"     => $Username, 
                    "doppelme_shadow_key"     => $ShadowKey,
                    "is_test"         => false
                    );
        $sReturnXML    = PartnerServiceCall(sURL, array($sDATA) );        
        
        if ( $sReturnXML == "" ) {
            //invalid call - problem
        } else {
            $xmlDoc         = simplexml_load_string( $sReturnXML->CreateDoppelMeAccountResult );
            $sDOPPELME_KEY        = $xmlDoc->DoppelMeKey;
            $sVALIDATION_KEY    = $xmlDoc->ValidationKey;
            $sSTATUS        = $xmlDoc->StatusCode;    
            
                        
            if ( $sSTATUS != 0 ) {
                //error message returned - perhaps invalid shadow key supplied                
                die();
            } else    {
                    //sanitize user input        
                $sDOPPELME_KEY = CleanTaintedInput($sDOPPELME_KEY);                    
                if ( $sDOPPELME_KEY != "" and $sDOPPELME_KEY != "N/A" ) {
                        //save the users primary doppelme key to your database (sql code below is example only)    
                    //we do this even though the user may select a different avatar next
                    /*
                    sSQL = "UPDATE [Your UserTable] SET DoppelMeKey='" . $sDOPPELME_KEY . "' WHERE UserID=" . $USER_ID
                    oDB.Execute sSQL
                    */
                }
                
                //loop through avialable avatars (user may have more than one) and format them, offering
                //user to select which one he would like to use on your site

                $iCount = 0;
                foreach ($xmlDoc->DoppelMeNames->xpath("//DoppelMeName") as $item) {
                        
                      echo "<h2>" . $item->title . "</h2>";
                    echo "<p>" . $item->description . "</p>";
                    
                    $sName = "";
                    $sKey = "";
                    
                    if ($sKey == $sDOPPELME_KEY) {
                        $sSELECT = $sSELECT  . "<td><img src=" . $DOPPELME_SITE . "/50/" . urlencode($sKey) . "/cropb.png><br>"; 
                        $sSELECT = $sSELECT  . $sName . "<br><input type=radio name=doppelme_key value=\"" . $sName . "\" checked></td>";
                    } else {
                        $sSELECT = $sSELECT  & "<td><img src=" . $DOPPELME_SITE . "/50/" . urlencode(sKey) . "/cropb.png><br>" .
                        $sName . "<br><input type=radio name=doppelme_key value=\"" . $sName . "\" ></td>";
                    }
    
                    $iCount++;
                } 
                
                $sSELECT = "<table><tr>" . $sSELECT . "</tr><tr><td colspan=" . $iCount . "><input type=submit></td></tr></table>";                
            }        
        }    
        
    }
            
?>

<html>
<head>
</head>
<body>
    
        <?php if ($sSELECT) { ?>
            Choose which of your avatars you want to use on this site
            <form action="/profile_avatar_create.asp" method="post" >
            <?php echo $sSELECT; ?> 
            </form>
        <?php } else { ?>

            <h3>Create a new DoppelMe Avatar</h3>
            <form action="profile_avatar_create.php" method="post">
            <table width="400">
            <tr><td colspan="2">
            Design your own avatar now. We use the <a href="http://www.doppelme.com">DoppelMe Avatar Engine</a> to create and manage avatars.
            </td></tr>
            <tr><td>Choose label for avatar <input type="text" name="avatar_name" value=""></td></tr>
            <tr><td colspan="2" align="center">
            <input type="hidden" name="create_new" value="Y" >
            <input type="submit"  value="Create &gt;" >
            </td></tr>
            </table>
            </form>


            <br>
            <h3>Already Have a DoppelMe Avatar?</h3>
            <form action="profile_avatar_create.php" method="post" >
            <table width="400">
            <tr><td colspan="2">If you already have an avatar from DoppelMe, you can use it on this site too. 
            Your shadow key can be obtained from the <strong>settings</strong> page on the <a href="http://www.doppelme.com">DoppelMe site</a>.
            <tr><td>doppelme username    </td><td> <input type="text" name="username" ></td></tr>
            <tr><td>doppelme shadow key    </td><td> <input type="text" name="shadowkey" ></td></tr>
            <tr><td colspan="2" align="center"><input type="submit" value="Use this avatar &gt; " ></td></tr>
            </table>
            <br><br>


        <?php }?>
    
</body>        
</html>