title: How to properly set content length and base64 url encoding in curl php
body: <p>Am trying to make curl request to an api using parameters below</p>
  
  <pre><code>POST /your api endpoint
  
  Content-type: x-www-form-urlencoded
  Content-Length: &lt;length of the request body&gt;
  [Authorization: Basic &lt;encoded client_id:client_secret string&gt;]
  
  [&amp; client_id=&lt;your client id&gt;]
  [&amp; client_secret=&lt;your client secret&gt;]
  </code></pre>
  
  <p><strong>This is what the documentation said:</strong> </p>
  
  <p>The client id and secret can also be sent in the Authorization header by encoding
  the <code>client_id&gt;:&lt;client_secret&gt;</code> string using base64. </p>
  
  <p><strong>My question</strong> is how do I get the requested content-length and also base64 encode the clientid and client secret in the curl header</p>
  
  <p><strong>Here is my effort so far.</strong></p>
  
  <pre><code>$id = 'xxxx';
  $secret='xxxx';
  
  $url = "//myapi.com/endpoint";
  $ch = curl_init();
  curl_setopt($ch,CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([
  
      'client_id'=&gt;$id,
      'client_secret'=&gt;$secret
  ]));
  
  curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                        
    'Content-Type: application/x-www-form-urlencoded',  
    'Content-Length: ',                                                                          
    "Authorization: Basic")                                                                       
  );  
  echo $data = curl_exec($ch);
  
  curl_close($ch);
  </code></pre>