title: How to update segments and groups of an existing subscriber in MailChimp API 3
body: <p>Iv'e written a method to subscribe users to MailChimp. Whats 'special' about it is that it automatically subscribe the users to groups within the list, and segments within the list, based on the users' cart items, wishlist items, and the item and / or category that he has subscribed from.</p>
  
  <p>The integration with MailChimp is straight forward - I get the data > send curl > get response > handle response.</p>
  
  <p>I'm looking for a way to constant update the users' groups and segments, based on their actions in my store.</p>
  
  <p>Now, the only accepted statuses MailChimp can get are 'subscribed', 'pending', and 'clean'. All of them aren't updating, only inserting new subscribers. If the email is already subscribed, nothing is being updated, not even data that is different than what the subscriber has in its profile in my MailChimp lists.</p>
  
  <p>Here's my code for reference:</p>
  
  <pre><code>    protected static function subscribeToMailchimp($email, $fullname)
  {
      $params     = EkerbaseJoomla::getPluginParams('system', 'ekerbaseusers');
      $interests  = self::getUserInterestsObject();
  
      $apikey                 = $params-&gt;mailChimpApiKey;
      $listId                 = $params-&gt;mailChimpListId;
      $interestCategoryId     = $params-&gt;mailChimpInterestCategoryId;
  
      $auth                   = base64_encode( 'user:' . $apikey );
      $apiUrl                 = 'https://'.substr($params-&gt;mailChimpApiKey, -3).'.api.mailchimp.com/3.0/lists/'.$listId;
      $possibleGroups         = json_decode(file_get_contents($apiUrl . '/interest-categories/' . $interestCategoryId . '/interests?apikey=' . $apikey))-&gt;interests;
      $segments               = json_decode(file_get_contents($apiUrl . '/segments?apikey=' . $apikey))-&gt;segments;
  
      $data = [
          'apikey'            =&gt; $apikey,
          'email_address'     =&gt; $email,
          'status'            =&gt; 'subscribed',
          'merge_fields'      =&gt;
              [
                  'FNAME'     =&gt; $fullname
              ]
      ];
  
      if( ! empty($interests-&gt;categories) ) {
  
          $data['interests'] = [];
  
          foreach( $possibleGroups as $group ) {
  
              if( in_array($group-&gt;name, $interests-&gt;categories) ) {
                  $data['interests'][$group-&gt;id] = true;
              }
  
          }
  
      }
  
      if( ! empty($interests-&gt;items) ) {
  
          $data['segments'] = [];
  
          foreach( $segments as $segment ) {
  
              if( in_array($segment-&gt;name, $interests-&gt;items) ) {
                  $data['segments'][$segment-&gt;id] = true;
              }
  
          }
  
      }
  
      $ch = curl_init();
  
      curl_setopt($ch, CURLOPT_URL, $apiUrl . '/members/');
      curl_setopt($ch, CURLOPT_HTTPHEADER,
          [
              'Content-Type: application/json',
              'Authorization: Basic '.$auth
          ]
      );
  
      curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/3.0');
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_TIMEOUT, 10);
      curl_setopt($ch, CURLOPT_POST, true);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
      curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
  
      $result     = curl_exec($ch);
      $response   = json_decode($result);
  
      switch( $response-&gt;status ) {
  
          case 'subscribed':
              $responseMessage = JText::_('EKERBASE_SUCCESS_NEWSLETTER');
              $responseStatus  = 'success';
              $responseResult  = 'mailchimp subscribing succeeded';
              break;
  
          default:
  
              $responseStatus  = 'error';
              $responseMessage = $response-&gt;title;
              $responseResult  = 'mailchimp subscribing failed';
  
              if( $response-&gt;title === 'Member Exists' ) {
                  $responseMessage = JText::_('EKERBASE_NEWSLETTER_ALREADY_SUBSCRIBER');
              }
  
              break;
  
      }
  
      return EkerbaseAjax::buildJsonResponse($responseMessage, $responseStatus, $responseResult);
  }
  </code></pre>