doppelme logo

Example Integration Code

Below are 4 example ASP 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 ASP - but the same prinicples can be applied to code in other languages/frameworks.

doppelme.asp | profile_avatar.asp | profile_avatar_create.asp | profile_avatar_updated.asp

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.
<!-- #include virtual="/include/doppelme.asp"-->
<html>
<head>
</head>
<body>
<%
            
    USER_ID     = Session("UserID")         '//your user's id                            
    Action      = Request.QueryString("a")
        
    
    Set oHTTP  = Server.CreateObject("Msxml2.ServerXMLHTTP")
    set xmlDoc = CreateObject("MSXML.DOMDocument")
    
    '//-------------------------------------------------------------
    '// User has requested to create a new avatar
    '//-------------------------------------------------------------
    if Request("create_new") = "Y" and len(Request("avatar_name")) > 0 then
        '//creates a new account and returns us a doppelme key
        avatar_name = Request("avatar_name")                
        sURL    = "CreateDoppelMeAccount"    
        sDATA    = "partner_id=" & DM_PARTNER_ID & "&partner_key=" & DM_PARTNER_KEY & "&partner_user_id=" & USER_ID & "&referral_id=0&doppelme_name=" & Server.URLEncode(avatar_name) & "&callback_url=&is_test=false"                
        sReturnXML = PartnerServiceCall(sURL, sDATA)
        
        if len(sReturnXML) = 0 then        
           '//problem?   
        else            
            xmlDoc.async = false
            xmlDoc.LoadXML(sReturnXML)            
            set xmlRoot = xmlDoc.documentElement    
            
            sDOPPELME_KEY    = xmlRoot.selectSingleNode("DoppelMeKey").Text
            sVALIDATION_KEY    = xmlRoot.selectSingleNode("ValidationKey").Text
            sSTATUS            = xmlRoot.selectSingleNode("StatusInfo").Text
            
            if len(sDOPPELME_KEY) > 0 and sDOPPELME_KEY <> "N/A" then
                '//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
                oDB.Execute sSQL
                
                Response.Redirect "/profile_avatar.asp"
            end if                    
        end if    
        
    '//-------------------------------------------------------------
    '// User has selected one of their avatar keys to use on your site
    '//-------------------------------------------------------------    
    elseif len(Request("doppelme_key")) >0 then
        
        sDOPPELME_KEY = Request("doppelme_key")
        '//always sanitize user input. DM keys should only ever consist of alpha-numeric values
        sDOPPELME_KEY = CleanTaintedInput(sDOPPELME_KEY)
        
        sSQL = "UPDATE [YourUserTable] SET DoppelMeKey='" & sDOPPELME_KEY & "' WHERE UserID=" & USER_ID
        oDB.Execute sSQL
                
        Response.Redirect "/profile_avatar.asp"        

    '//-------------------------------------------------------------
    '// User alrady 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 len(Request("username"))>0 then
            
        sURL        = "AssignPartnerUserID"            
        sDATA        = "partner_id=" & DM_PARTNER_ID & "&partner_key=" & DM_PARTNER_KEY & "&partner_user_id=" & USER_ID & "&doppelme_username=" & Request("username") & "&doppelme_shadow_key=" & Request("shadowkey") & "&is_test=false"
        sReturnXML    = PartnerServiceCall(sURL, sDATA)        
        
        if len(sReturnXML) = 0 then
            '//invalid call
            Response.End
        else
            xmlDoc.async = false
            xmlDoc.LoadXML(sReturnXML)            
            set xmlRoot = xmlDoc.documentElement    
            
            sDOPPELME_KEY    = xmlRoot.selectSingleNode("DoppelMeKey").Text
            sVALIDATION_KEY    = xmlRoot.selectSingleNode("ValidationKey").Text
            sSTATUS_CODE    = xmlRoot.selectSingleNode("StatusCode").Text            
            
            
            if CLng(sSTATUS_CODE) <> 0 then
                '//error message returned - perhaps invalid shadow key supplied                
                Response.End
            else    
                '//sanitize user input        
                sDOPPELME_KEY = CleanTaintedInput(sDOPPELME_KEY)                    
                if len(sDOPPELME_KEY) > 0 and sDOPPELME_KEY <> "N/A" then
                    '//save the users doppelme key to your database (sql code below is example only)
                    sSQL = "UPDATE [Your UserTable] SET DoppelMeKey='" & sDOPPELME_KEY & "' WHERE UserID=" & USER_ID
                    oDB.Execute sSQL
                end if
                
                '//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
                set xmlNode = xmlRoot.selectSingleNode("DoppelMeNames")
                set xmlNodes = xmlNode.selectNodes("//DoppelMeName")            
                iCount = 0
                for i = 0 to xmlNodes.Length - 1
                
                    sName    = xmlNodes.item(i).attributes(0).text
                    sKey    = xmlNodes.item(i).text 
                    
                    if sKey = sDOPPELME_KEY then
                        sSELECT = sSELECT  & "<td><img src=" & DOPPELME_SITE & "/50/" & Server.URLEncode(sKey) & "/cropb.png><br>" & _
                        Server.HTMLEncode(sName) & "<br><input type=radio name=doppelme_key value=""" & Server.HTMLEncode(sName) & """ checked></td>"
                    else
                        sSELECT = sSELECT  & "<td><img src=" & DOPPELME_SITE & "/50/" & Server.URLEncode(sKey) & "/cropb.png><br>" & _
                        Server.HTMLEncode(sName) & "<br><input type=radio name=doppelme_key value=""" & Server.HTMLEncode(sName) & """ ></td>"
                    end if
                    iCount = iCount + 1
                    
                    if iCount = 4 then
                        sROW = sROW & "<tr>" & sSELECT & "</tr>"
                        sSELECT = ""
                        iCount = 0
                    end if                                        
                next
                
                if iCount > 0 then
                    for i = iCount + 1 to 4
                        sSELECT = sSELECT & "<td></td>"
                    next
                    sROW = sROW & "<tr>" & sSELECT & "</tr>"
                end if
                
                if len(sROW) > 0 then            
                    sSELECT = "<table class=data>" & sROW & "<tr><td colspan=4><input type=submit></td></table>"
                end if
            end if        
        end if    
        
    end if
            
%>
    
        <%if len(sSELECT) > 0 then%>
            Choose which of your avatars you want to use on this site
            <form action="/profile_avatar_create.asp" method="post" >
            <%=sSELECT%> 
            </form>
        <%else%>

            <h3>Create a new DoppelMe Avatar</h3>
            <form action="profile_avatar_create.asp" 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> <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.asp" 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>


        <%end if%>
    
</body>        
</html>