title: Convert URL encoded to JSON object
body: <p>I've never worked with JSON objects before but I think this is probably the best route for what I'm trying to achieve (if you think there is a better option please let me know, though I cannot use SOAP)</p>
  
  <p>I am trying to submit some data via POST to an API in order to add some data to a newsletter list which is stored as XML. Here is the documentation for the request I am trying to complete: <a href="https://api.dotmailer.com/v2/help/wadl#PostAddressBookContacts" rel="nofollow">https://api.dotmailer.com/v2/help/wadl#PostAddressBookContacts</a></p>
  
  <p>When visiting the API url and authenticating I see data laid out in the following:</p>
  
  <pre><code>&lt;ArrayOfApiContact xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://apiconnector.com"&gt;
    &lt;ApiContact&gt;
      &lt;Id&gt;1058905201&lt;/Id&gt;
      &lt;Email&gt;email@email.co.uk&lt;/Email&gt;
      &lt;OptInType&gt;Unknown&lt;/OptInType&gt;
      &lt;EmailType&gt;Html&lt;/EmailType&gt;
      &lt;DataFields i:nil="true"/&gt;
      &lt;Status&gt;Subscribed&lt;/Status&gt;
    &lt;/ApiContact&gt;
  &lt;/ArrayOfApiContact&gt;
  </code></pre>
  
  <p>So I have attempted to replicate as such, using a variable instead of a hardcoded email:</p>
  
  <pre><code>$xml = array(
      'id' =&gt; urlencode('123456789'),
      'email' =&gt; urlencode($useremail),
      'optInType' =&gt; urlencode('Unknown'),
      'emailType' =&gt; urlencode('Html'),
      'dataFields' =&gt; urlencode(':null'),
      'status' =&gt; urlencode('Subscribed')
  );
  </code></pre>
  
  <p>I am combining the array here:</p>
  
  <pre><code>foreach($xml as $key=&gt;$value) { $xml_string .= $key.'='.$value.'&amp;'; }
  </code></pre>
  
  <p>And then using cURL to POST the request to the API, with my authentication (<code>apipassword</code> and <code>apiemail</code> which the username)</p>
  
  <pre><code>$ch = curl_init();
  curl_setopt($ch, CURLOPT_URL,$auth_url);
  curl_setopt($ch,CURLOPT_POST, count($xml));
  curl_setopt($ch,CURLOPT_POSTFIELDS, $xml_string);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
  curl_setopt($ch, CURLOPT_USERPWD, "$apiemail:$apipassword");
  $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);   //get status code
  $result = curl_exec ($ch);
  curl_close ($ch);
  </code></pre>
  
  <p>I have already tested this without the POST and I do indeed get the data back in this form:</p>
  
  <pre><code>[{"id":1058905201,"email":"email@email.co.uk","optInType":"Unknown","emailType":"Html","dataFields":null,"status":"Subscribed"}]
  </code></pre>
  
  <p>So I know the auth is working and the data is coming back correctly, but now when I try to POST my data I get:</p>
  
  <pre><code>{"message":"Content type \"application/x-www-form-urlencoded\" is not supported. Please use one of: application/json,application/xml ERROR_CONTENT_TYPE_IS_NOT_SUPPORTED"}
  </code></pre>
  
  <p>So how do I convert my array which is URL encoded into something readable by the API?</p>