title: Adding subscribers to a list using Mailchimp's API v3
body: <p>I'm trying to add users to a list I've created in Mailchimp but I can't find any code examples anywhere. I've tried figuring out how to use the API but I'm very much a "Look at an example and learn" kind of person.</p>
  
  <p>I've tried using version 2 of the API but nothing seems to be working despite working from examples on the net and Mailchimp says the following about earlier versions of their API on their website:</p>
  
  <blockquote>
    <p>Versions 2.0 and earlier are deprecated. Only minimal support—bug fixes, security patches—will be available for those versions.</p>
  </blockquote>
  
  <p><strong>UPDATE 1</strong>: I did some further research based on TooMuchPete's <a href="https://stackoverflow.com/a/30492435/2443113">answer</a> with regards to the link on Managing Subscribers and altered some code I found <a href="https://stackoverflow.com/a/2445332/2443113">here</a>, but it won't work because the function <a href="https://php.net/http_build_query" rel="noreferrer">http_build_query()</a> doesn't deal with nested arrays. I'm not sure how to deal with the 'merge_fields' portion of adding a subscriber. My current code is below:</p>
  
  <pre><code>$postdata = http_build_query(
                      array(
                          'apikey'        =&gt; $apikey,
                          'email_address' =&gt; $email,
                          'status'        =&gt; 'subscribed',
                          'merge_fields'  =&gt; array(
                              'FNAME' =&gt; $name
                          )
                      )
                  );
  
                  $opts = array('http' =&gt;
                      array(
                          'method'  =&gt; 'POST',
                          'header'  =&gt; 'Content-type: application/x-www-form-urlencoded',
                          'content' =&gt; $postdata
                      )
                  );
  
                  $context  = stream_context_create($opts);
  
                  $result = file_get_contents('https://us2.api.mailchimp.com/3.0/lists/&lt;list_id&gt;/members/', false, $context);
  
                  var_dump($result);
                  die('Mailchimp executed');
  </code></pre>
  
  <p><strong>UPDATE 2</strong>: I've now resorted to using curl and I've managed to get something almost working. The data sends through to Mailchimp but I'm receiving the error <strong><em>"Your request did not include an API key."</em></strong> I'm guessing I need to authenticate as mentioned <a href="http://kb.mailchimp.com/api/article/api-3-overview" rel="noreferrer">here</a>. I've tried adding it to the http header which hasn't worked. See my code below:</p>
  
  <pre><code>$apikey = '&lt;api_key&gt;';
                  $auth = base64_encode( 'user:'.$apikey );
  
                  $data = array(
                      'apikey'        =&gt; $apikey,
                      'email_address' =&gt; $email,
                      'status'        =&gt; 'subscribed',
                      'merge_fields'  =&gt; array(
                          'FNAME' =&gt; $name
                      )
                  );
                  $json_data = json_encode($data);
  
                  $ch = curl_init();
                  curl_setopt($ch, CURLOPT_URL, 'https://us2.api.mailchimp.com/3.0/lists/&lt;list_id&gt;/members/');
                  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json/r/n
                                                              Authorization: Basic '.$auth));
                  curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-MCAPI/2.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_data);                                                                                                                  
  
                  $result = curl_exec($ch);
  
                  var_dump($result);
                  die('Mailchimp executed');
  </code></pre>