title: how to properly use Mailgun Api via Curl in php
body: <p>I have the curl request below from mailgun which i want to use to create an email list for newsletters on my site.</p>
  
  <pre><code> curl -s --user 'api:YOUR_API_KEY' \
         https://api.mailgun.net/v3/lists \
         -F address='LIST@YOUR_DOMAIN_NAME' \
         -F description='Mailgun developers list'
  </code></pre>
  
  <p><strong>My issues</strong> is that when I run the code below, it shows <strong>invalid private key 401</strong>
  It seems that am not setting the api key properly in the curl request. below is my coding so far</p>
  
  <pre><code>&lt;?php
  
  $email = 'tmack@gmail.com';
  $api_key = 'my key here';
  $api = 'api:';
  $api_k = $api.$api_key;
  $url = 'https://api.mailgun.net/v3/lists';
  
  $json = json_encode([
      'address' =&gt; $email,
      'description'        =&gt; 'Tmack group Mailing List'
  ]);
  
  
  $ch = curl_init($url);
  //curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $api_key);
  
  curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $api_k);
  curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
  $result = curl_exec($ch);
  $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  echo $result;
  curl_close($ch);
  echo $status_code;
  </code></pre>