Creating a new user with the Rest API - Affiliate

Discussion in 'Customization & add-ons' started by jigsawtrading, Jun 4, 2019.

  1. jigsawtrading

    jigsawtrading aMember Pro Customer

    Joined:
    Sep 28, 2012
    Messages:
    24
    Hi

    I have an affiliate auto-creating users on my members system (indirectly, via our application API).

    What we want to do is identify if the user is new to us and if so, assign their affiliate id to that user.

    We are following the guide to adding a user via API here: http://www.amember.com/docs/REST_API_examples

    A few questions
    1 - If the user already exists - will it be updated or will it be ignored? We prefer to ignore an existing user.
    2 - Can we add the affiliate id to any newly created customers? If so - how do we do that? Which columns would we need to populate & what is the right data?

    Many thanks

    Peter
  2. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    2,295
    Hello,

    1. In event of you will try to insert user with username that already exists then you get error
    2. You need to set column aff_id to id of affiliate (user_id)

    Best Regards.
  3. jigsawtrading

    jigsawtrading aMember Pro Customer

    Joined:
    Sep 28, 2012
    Messages:
    24
    Thanks Caesar

    I am struggling with that. I think I did what you said....

    PHP:
     public function CreateUser($empUsername,$empPassword,$empEmail,$empFirstName,$empLastName,$compUsername){
        
            
    Log::info('create user '.$empUsername." Affiliate ".$compUsername);
            
    $url        $this->member_url_api.'users';
            
    $key        self::$AUTH_KEY;
            
    $fields = array(
                
    '_key'      => $key,
                
    'login'     => $empUsername,
                
    'pass'      => $empPassword,
                
    'email'     => $empEmail,
                
    'name_f'    => $empFirstName,
                
    'name_l'    => $empLastName,
                
    'aff_id'    => $compUsername,
            );
           
            
    // Register new user to aMembersystem with REST API.
            
    $query http_build_query($fields);
            
    $query str_replace("%40""@"$query);

            
    $header = array("Content-type: application/x-www-form-urlencoded");
            
    //open connection
            
    $ch curl_init();
            
    //set the url, number of POST vars, POST data
            
    curl_setopt($chCURLOPT_URL$url);
            
    curl_setopt($chCURLOPT_HTTPHEADER$header);
            
    curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
            
    curl_setopt($chCURLOPT_POST1);
            
    curl_setopt($chCURLOPT_POSTFIELDS$query);
            
    curl_setopt($chCURLOPT_SSL_VERIFYPEER0);

            
    //execute post
            
    $result curl_exec($ch);
            
    //close connection
            
    curl_close($ch);

            
    $jsonData json_decode($result,true);
            if(
    $jsonData){
             
    $content =  array_values($jsonData)[0];
              if(
    is_array($content)){
                return 
    'useradded';
              }
            }else{
               return 
    'apinotworking';
            }
        }

    As you can see there's a log at the top. I ran it, the user got created perfectly but without an assigned affiliate.

    [2019-06-05 15:46:34] local.INFO: create user Test3001 Affiliate JigsawTrading

    User "JigsawTrading" is an assigned affiliate but the referred Affiliate is still empty

    referred.png

    Any ideas what is wrong?

    Cheers

    Pete
  4. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    2,295
    aff_id should be integer value, it is user_id column from table am_user for your affiliate.
  5. jigsawtrading

    jigsawtrading aMember Pro Customer

    Joined:
    Sep 28, 2012
    Messages:
    24
    Got it - works a treat! Thanks!
  6. caesar

    caesar aMember Pro Developer Staff Member

    Joined:
    Oct 16, 2009
    Messages:
    2,295
    I am glad to hear that. You are welcome!

Share This Page